如何在angularjs中设置一个范围object.object的值

时间:2014-11-19 13:10:51

标签: angularjs angularjs-scope

如何设置值并以 $ scope.object1.object ="某些数据";

的形式获取值

在html和js中,如果我设置这样的数据,它将起作用。

<select ng-model="myColor" ng-options="color.name for color in colors">

 $scope.colors = [
          {name:'black', shade:'dark'},
          {name:'white', shade:'light'},
          {name:'red', shade:'dark'},
          {name:'blue', shade:'dark'},
          {name:'yellow', shade:'light'}
        ];

但实际上我想要这些

 <select ng-model="myColor" ng-options="color.name for color in test.colors">

  $scope.test.colors = [
          {name:'black', shade:'dark'},
          {name:'white', shade:'light'},
          {name:'red', shade:'dark'},
          {name:'blue', shade:'dark'},
          {name:'yellow', shade:'light'}
        ];

请参阅plunker demo

中的示例

1 个答案:

答案 0 :(得分:2)

请参阅此plunkr

angular.module('selectExample', [])
  .controller('ExampleController', ['$scope', function($scope) {
    // ADD THIS NEXT LINE
    $scope.test = {}
    $scope.test.colors = [
      {name:'black', shade:'dark'},
      {name:'white', shade:'light'},
      {name:'red', shade:'dark'},
      {name:'blue', shade:'dark'},
      {name:'yellow', shade:'light'}
    ];
    $scope.myColor = $scope.test.colors[2]; // red
  }]);