我有以下HTML:NG選項設置爲NG-模型對象
<div class="row" ng-repeat="question in currentTemplate.questions">
<div class="col-xs-4" ng-show="editingQuestion[$index]">
<select style="width:100%;" ng-model="editingCurrentlySelectedAnswerOption[$index]"
ng-options="answerOption as answerOption.back for answerOption in answerOptions">
</select>
</div>
</div>
我想知道我是怎麼設置的什麼在SELECT語句顯示的值。當頁面加載時,它應該已經被設置,因爲我通過一個for循環來設置editingCurrentlySelectedAnswerOption的值,以便每一個都被預先選擇([$ index]指的是一個ng-repeat的索引,它在裏面由於在ng-repeat中有多個選擇,我需要在數組中每個ng-repeat需要一個點來跟蹤每個單獨的選擇),但是首先它會作爲空白點出現。我已經使用了ng-init,這是一個當按下按鈕來取消隱藏上面的div時調用的函數,以及在頁面加載時的函數。我使用DOM和console.logs中的綁定來檢查值。他們似乎都是對的。在NG-選項answerOptions是:
var answerOptions = [
{ front: "RadioButton", back: "Multi-Choice" }
{ front: "CheckBox", back: "Multi-Answer" }
{ front: "TextBox", back: "Free Text" }
];
當我打印的內容editingCurrentlySelectedAnswerOption [$指數]是它永遠是正確顯示就像上述目的的一個對象,但由於某些原因,它始終是一個空白加載時選擇語句。有什麼我不知道ng-options如何處理對象?還是我在做其他事情?
UPDATE:
我設置editingCurrentlySelectedAnswerOption的值是這樣的:
this.scope.editingCurrentlySelectedAnswerOption = [];
for (var i in this.scope.currentTemplate.questions) {
if (this.scope.currentTemplate.questions.hasOwnProperty(i)) {
this.scope.editingCurrentlySelectedAnswerOption.push(this.scope.currentTemplate.questions[i].answerType);
}
}
的原因,我遞增的倍量有在this.scope.currentTemplate問題,是因爲在ng-repeat的html也重複了大量的問題。他們應該匹配。
,這是this.scope.currentTemplate.questions [0]如何定義:
new Utilities.Question(
"Question1",
{ front: "TextBox", back: "Free Text" },
["Yes", "Maybe", "No", "I don't know"],
[50.0, 25.0, 0.0, -25.0]
)
,這裏是一個Utilities.Question的定義:
export class Question {
question: string;
answerType: any;
answerOptions: string[];
answerWeights: number[];
constructor(question?: string, answerType?: any, answerOptions?: string[], answerWeights?: number[]) {
this.question = question;
this.answerType = answerType;
this.answerOptions = answerOptions;
this.answerWeights = answerWeights;
}
}
你能告訴你如何設置'editingCurrentlySelectedAnswerOption'的值嗎?僅僅因爲對象具有相同的屬性並不意味着它們是平等的[(請參閱示例)](http://jsfiddle.net/qb6rsm1m/)。 – Stryner 2014-10-10 20:54:16
我更新了它,使其更加具有描述性,代價很大。 – 2014-10-10 21:05:03
你可以在'Question'中使用string而不是'answerType'對象嗎?然後,將選項綁定到'answerOption.front作爲answerOptionsback for answerOption in answerOptions'。在我看來,@尼斯有這個觀點。 – nrodic 2014-10-10 21:09:19