如何将 ng-options comprehension_expression 传递给 AngularJS 组件?

How to pass ng-options comprehension_expression to AngularJS component?

我正在尝试向现有 AngularJS 组件添加一个新绑定,该组件应采用 comprehension_expression 类型的值,如 ng-options Directive API Reference.

中所述

请查看下方代码了解情况。请注意,顶部 <select> 控件来自名为 selectField 的组件。它不显示任何 select 选项。底部控件直接添加到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>

参见

在这种情况下,ng-options 指令将在插值指令呈现所需表达式之前解析理解表达式。

重新编写组件以输入选项:

app.component('selectField', {
    require: {ngModelCtrl: 'ngModel'},
    bindings: {
      ngModel: '<',
      choices: '<'
    },
    template: `
      <select ng-model="$ctrl.ngModel"
              ng-change="$ctrl.render($ctrl.ngModel)"
              ̶n̶g̶-̶o̶p̶t̶i̶o̶n̶s̶=̶"̶{̶{̶:̶:̶$̶c̶t̶r̶l̶.̶i̶n̶p̶u̶t̶O̶p̶t̶i̶o̶n̶s̶E̶x̶p̶r̶e̶s̶s̶i̶o̶n̶}̶}̶"̶ ̶
              ng-options="c for c in choices">
      </select>
      Selected: {{$ctrl.ngModel}}</span>
    `,
    controller: function() {
        this.render = (value) => {
            this.ngModelCtrl.$setViewValue(value);
        };
    }
})

用法:

<select-field ng-model="vm.myColor" choices="vm.colors">
</select-field>

演示

angular.module('myApp', [])
.controller('MainController', function MainController() {
    this.colors = ['red', 'blue', 'green'];
    this.myColor = this.colors[1]; // blue
})
.component('selectField', {
    require: {ngModelCtrl: 'ngModel'},
    bindings: {
      ngModel: '<',
      choices: '<'
    },
    template: `
      <fieldset>Select field
      <select ng-model="$ctrl.ngModel"
              ng-change="$ctrl.render($ctrl.ngModel)"
              ng-options="c for c in $ctrl.choices">
      </select>
      Selected: {{$ctrl.ngModel}}
      </fieldset>
    `,
    controller: function() {
        this.render = (value) => {
            this.ngModelCtrl.$setViewValue(value);
        };
    }
})
<script src="//unpkg.com/angular/angular.js"></script>
<body ng-app="myApp" ng-controller="MainController as vm">
    <div>
      <select-field ng-model="vm.myColor"
                    choices="vm.colors">
      </select-field>
    </div>
    <div>
      <select ng-model="vm.myColor" 
              ng-options="color for color in vm.colors">
      </select>
      Selected: {{vm.myColor}}
    </div>
</body>