2015-06-22 107 views
0

我使用以下select。目前,我在開始時選擇了空白選項。要避免angularJS中的這些空白選項,我想要預先選擇select中的第一個選項。但它不起作用。我得到'無法讀取屬性'的'未定義'的條件。感謝您的提示!預先選擇選項不起作用

HTML

<select ng-show="rule.field=='Cardiology'" ng-options="c.name as c.name for c in conditions" ng-model="rule.condition" class="form-control input-sm selectCondition"></select> 

JS模型

scope.conditions = [{ 
     name : 'contains', 
}, { 
     name : 'doesn´t contain', 
}]; 

$scope.conditions = {type : $scope.conditions[0].value}; 
+0

這是模糊的,你正在使用scope.conditions和$ scope.conditions(通知$) –

+0

你有scope.conditions = [{},{}],但你do $ scope.conditions = {type:$ scope.conditions [0] .value};它應該是$ scope.conditions = {type:scope.conditions [0] .value}; – Jax

+0

[引自ngOptions參考](https://docs.angularjs.org/api/ng/directive/ngOptions) '默認情況下,ngModel通過引用觀察模型,而不是值。將任何輸入指令綁定到作爲對象或集合的模型時,這很重要。'你需要設置'$ scope.condition = $ scope.conditions [anyConditionYouWantToSet]' – nilsK

回答

1

綁定值獲得選擇。

嘗試這樣

$scope.rule.condition=$scope.conditions[0].value; 
+0

不幸的是,它不起作用我得到一個錯誤:TypeError:無法讀取Scope.compile.scope.changeParameter中未定義 的屬性「規則」 angular.js:357) at $ parseFunctionCall(angular.js:12345) at Scope。$ get.Scope。$ eval(angular.js:14401) at angular.js:20541 at angular。 JS:23184 在的forEach(angular.js:323)。在 $$ NgModelController writeModelToScope(angular.js:23182) 在writeToModelIfNeeded(angular.js:23175) 在angular.js:23169 在validationDone(角。 js:23097) – mm1975

+0

認真嗎?你的$範圍沒有定義?你需要解決這個問題.. 答案正是你想要的。 –