如何设置值并以 $ 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
中的示例答案 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
}]);