使用控制器时使用ngOptions

Chi*_*tus 0 angularjs

他全部,

我正在尝试使用angular ngOptions和controllerAs选项,但它不起作用.我做错了什么?

Chaim,先谢谢大家

我的HTML是:

<body ng-app="root">
    <div ng-controller="TestCtrl as tests">
        <select ng-options="test.name for test in tests.list">
        </select>
    </div>
    <script src="angular.js"></script>
     <script>
    angular.module("root",[])
        .controller("TestCtrl", function(){
            this.list=[
                {name:"a"},
                {name:"b"}
            ];
        });
</script> 
</body>
Run Code Online (Sandbox Code Playgroud)

Ily*_*nin 5

发生这种情况是因为您没有指定强制性ng-model属性,如下所示:

<select ng-model="tests.selected" ng-options="test as test.name for test in tests.list">
</select>
Run Code Online (Sandbox Code Playgroud)

并在您的控制器中:

...
this.selected = this.list[0];
...
Run Code Online (Sandbox Code Playgroud)

见演示