【发布时间】:2018-08-09 01:33:05
【问题描述】:
我正在尝试向现有 AngularJS 组件添加一个新绑定,该组件应采用 comprehension_expression 类型的值,如 ng-options Directive API Reference 中所述。
请检查底部的代码以了解情况。请注意,顶部的<select> 控件来自名为selectField 的组件。它不显示任何选择选项。底部控件直接添加到index.html,可以正常使用。
如果有人能告诉我我的脚本中是否存在错误,将值传递给模板的 ng-options 属性的任何替代方法,或者让我知道组件或指令无法有这样的绑定。
angular.module('myApp', [])
.controller('MainController', function MainController() {
this.colors = ['red', 'blue', 'green'];
this.myColor = this.colors[1]; // blue
}).component('selectField', {
template: `
<select ng-model="$ctrl.inputModel"
ng-options="{{::$ctrl.inputOptionsExpression}}">
</select>
Selected: {{$ctrl.inputModel}}</span>
`,
bindings: {
inputModel: '=',
inputOptionsExpression: '@'
}
});
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
</head>
<body ng-app="myApp">
<div ng-controller="MainController as vm">
<div>
<select-field input-model="vm.myColor"
input-options-expression="color for color in vm.colors">
</select-field>
</div>
<div>
<select ng-model="vm.myColor"
ng-options="color for color in vm.colors">
</select>
Selected: {{vm.myColor}}
</div>
</div>
</body>
</html>
【问题讨论】:
-
完成。包含问题的代码。
标签: angularjs angularjs-ng-options angularjs-components