Angularjs选择不传递值

时间:2016-02-18 09:23:06

标签: angularjs

我在表单中有一个选择下拉列表,它没有将值传递给{{service}}对象:

<select name="categoryId" ng-model="service.categoryId" ng-options="category.id as category.name for category in categories track by category.categoryId" required>
  <option value="0">-select a category-</option>
</select>

我无法找到原因,任何人?

1 个答案:

答案 0 :(得分:1)

检查此示例,它可能会对您有所帮助:

&#13;
&#13;
function theController($scope) {

  $scope.categories = [
    {name:'First', id:'A', categoryId: 1},
    {name:'Second', id:'B', categoryId: 2},
    {name:'Third', id:'C', categoryId: 3}
  ];
  
  $scope.service = $scope.categories[1];
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<body ng-app>
    
  <div ng-controller="theController">
    <select name="categoryId" 
            ng-model="service" 
            ng-options="category as category.name for category in categories track by category.categoryId" required>
      <option value="">-select a category-</option>
    </select>
    <pre>categories = {{categories|json}}</pre>
    <pre>service = {{service|json}}</pre>
  </div>
</body>
&#13;
&#13;
&#13;