简体   繁体   English

在指令中使用ng-model和ng-repeat

[英]Using ng-model and ng-repeat within a directive

So I can't seem to quite figure this out. 所以我似乎不太清楚这一点。 Basically, I am building a directive which consists of options, and I want to define those options in the directive, but define the default option in my controller. 基本上,我正在构建一个包含选项的指令,并且我想在指令中定义这些选项,但要在控制器中定义默认选项。 It seems to render fine, but the default option is not being selected. 看起来效果不错,但是未选择默认选项。 any hints? 有什么提示吗? here is the Plunkr 这是Plunkr

    angular.module('app', [])
  .controller('Ctrl', function ($scope) {
    $scope.defaultSearchRange = 3;  
  })
  .directive('dateRange', function () {
    return {
      restrict: 'A',
      scope: {
        searchRange: '='
      },
      replace: true,
      template: '<div><select ng-model="searchRange"><option ng-repeat="option in options" value="{{option.value}}">{{option.name}}</option></select></div>',
      controller: function ($scope) {
        $scope.options = [
            { name: '',             value: 0 },
            { name: 'Today',        value: 1 },
            { name: 'Yesterday',    value: 2 },
            { name: 'Last 7 Days',  value: 3 },
            { name: 'Last 30 Days', value: 4 },
            { name: 'Month to Date',value: 5 },
            { name: 'Year to Date', value: 6 }
        ]
      }
    }
  })

You are not able to set the default because ng-repeat by default will not take into account ng-model . 您无法设置默认值,因为默认情况下ng-repeat将不考虑ng-model

Here is a forked plunker that is working. 这是一个正在工作的分叉的矮人

I would suggest to use ng-options instead of ng-repeat . 我建议使用ng-options而不是ng-repeat The track by variation will allow you to set the selected value. track by变化可以让您设置选定的值。 It must be in the same "object format" to work though. 它必须具有相同的“对象格式”才能工作。

Here is the modified code: 这是修改后的代码:

angular.module('app', [])
  .controller('Ctrl', function ($scope) {
    $scope.defaultSearchRange = 3;
  })
  .directive('dateRange', function () {
    return {
      restrict: 'A',
      scope: {
        searchRange: '='
      },
      replace: true,
      template: '<div><select ng-model="selected" ng-options="o.name for o in options track by o.value"></select></div>',
      link: function (scope) {

        scope.selected = { value: scope.searchRange };

        scope.$watch('selected', function(selected) {

          scope.searchRange = selected.value;
        });

        scope.options = [
            { name: '',             value: 0 },
            { name: 'Today',        value: 1 },
            { name: 'Yesterday',    value: 2 },
            { name: 'Last 7 Days',  value: 3 },
            { name: 'Last 30 Days', value: 4 },
            { name: 'Month to Date',value: 5 },
            { name: 'Year to Date', value: 6 }
        ];
      }
    }
  })

Note: the above code is mapping into a new directive scope property selected . 注意:上面的代码映射到一个新的指令scope属性selected If the controller wanted to pass an object instead of a just the number value, this would not be necessary. 如果控制器想传递一个对象而不只是传递数字值,则没有必要。 Since the value is mapped in, we must $watch and then map the actual selected value back out. 由于该值已映射,我们必须先$watch然后将实际选择的值映射回去。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM