angularjs ng-options嵌套变量

时间:2014-06-12 04:55:45

标签: javascript angularjs select ng-options

Ng-options似乎不适用于$ scope上的对象。对于前$ scope.model.colors。 这是我的代码中的错误或错误。 http://jsfiddle.net/wbsrvc/uDteZ/#base

function Main($scope) {
 $scope.model.selected = "value1";
 $scope.model.colors = ["value1", "value2", "value3"];
}
<div ng-controller="Main" ng-app> <span>{{selected}}</span>

  <input ng-model="model.selected" />
  <select ng-model="model.selected" ng-options="v for v in model.colors"></select>
</div>

2 个答案:

答案 0 :(得分:3)

您缺少在范围

中创建模型对象
function Main($scope) {

 $scope.model = {};  //create model object in scope.
 $scope.model.selected = "value1";
 $scope.model.colors = ["value1", "value2", "value3"];
}

答案 1 :(得分:1)

告诉$ scope.model它是一个对象

function Main($scope) {
  $scope.model = {};
  $scope.model.selected = "value1";
  $scope.model.colors = ["value1", "value2", "value3"];
}

如果未定义模型,Angular无法找到模型