KnockoutJS 2.3.0 アプリに次の HTML があります。
<div data-bind="foreach: chosenSurvey().questions">
<pre data-bind="text: ko.toJSON($data.question, null, 2)"></pre>
<label data-bind="text: $data.question_type"></label>
<label data-bind="text: $data.question[0].content"></label>
<div>
<!-- ko if: question_type == 'input' -->
<input type="text" />
<!-- /ko -->
<!-- ko if: question_type == 'textarea' -->
<textarea></textarea>
<!-- /ko -->
</div>
次の出力が得られます。
[
{
"content": "Where are you from?",
"lang_id": 0
},
{
"content": "¿De donde está?",
"lang_id": 1
}
]
input
そしてコンソールエラー:
Uncaught Error: Unable to parse bindings.
Message: TypeError: Cannot read property 'content' of undefined;
Bindings value: text: $data.question[0].content
したがって、foreachは問題ないようです。デバッグ手法を使用して「質問」プロパティを表示できます。「question_type」プロパティが正しく出力されていることがわかります。しかし、何をしようとしても、質問の [0] 番目の要素にアクセスできません。question 配列内のオブジェクトはデバッグ中に出力されるように見えますが、直接アクセスしようとすると、question[0] は常に未定義です。
私の目標は、「言語」のメニューを選択することであり、ユーザーが望む言語で質問を表示します。では、質問配列の 1 つの特定の要素にどのようにアクセスすればよいでしょうか?
「if」ステートメントも機能しないので、これを完全に間違っているのではないかと思います。ありがとう!