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

时间:2018-08-09 01:33:05

标签: angularjs angularjs-ng-options angularjs-components

我正在尝试向现有的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>

1 个答案:

答案 0 :(得分:1)

请参见Why mixing interpolation and expressions is bad practice

在这种情况下,# SECURITY WARNING: don't run with debug turned on in production! DEBUG = True ALLOWED_HOSTS = ['127.0.0.1', 'omnibus.us-east-1.elasticbeanstalk.com'] 指令将在插值指令呈现所需表达式之前解析理解表达式。

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

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);
        };
    }
})