1

我有一个带有以下标记的 select2 下拉列表:

<select id="selectByName" ui-select2="select2Options" ng-model="selectId" data-placeholder="Select item by name" style="width:250px">
  <option></option>
  <option ng-repeat='item in items' data-show="{{item.show}}" value="{{item.id}}">
    {{item.name}}
  </option>       
</select>

js包含以下内容:

$scope.items(具有 id、布尔显示属性和名称属性的数组)

和选择2个选项:

select2Options : {
       allowClear: true,
       placeholder:"select a value",
       formatResult: function(state) {
          var $elem = angular.element(state.element),
          isVisible = $elem.data('show');
          return isVisible ? '<span style="color:red">'+state.text+'</span>':
                              <span style="color:blue">'+state.text+'</span>';

       }
 },

好吧,ng-repeat正确更新了 html 标记并将 data-show 属性设置为 true 或 false,但该formatResult函数不会更新此值。在html源代码data-show="true"formatResult函数$elem.data('show') = false;中,为什么每次打开select2时调用函数时它不更新?

这是一个说明我的问题的 plunker:plnkr.co/edit/d0LxuhzdQh7hMdzOoxpr?p=preview 。它看起来 formatResult 在第一次打开 select2 之前只正确更新了一次结果。

4

1 回答 1

1

编辑

http://plnkr.co/edit/6Vma1WTQWQw0HAIQUVxE?p=preview

  $scope.select2options = {
    allowClear: true,
    placeholder: "select a value",
    formatResult: function(state, container) {
      var $elem = angular.element(state.element);
      var scope = $elem.data('$scope');
      if (scope !== undefined) {
        isVisible = scope.$eval($elem.data('show'));
        $scope.dataShow[$elem.attr('value')] = isVisible;
        $scope.updated++;
        return isVisible ? '<span style="color:red">' + state.text + '</span>' :
          ' <span style="color:blue">' + state.text + '</span>'
      }
    }
  }

关键部分是$scope从 jqLit​​e 元素中获取数据,然后调用$eval,它在作用域的上下文中评估未解析的字符串表达式。如果我们使用$scope.$eval了 ,它将使用控制器$scope,而控制器上不会有ng-repeat。通过从元素中获取它,我们有一个可以访问item.ng-repeat

话虽如此,我不建议使用此代码(有时 jQuery 小部件在使用 Angular 时会迫使您陷入不愉快的角落)。同样,如果您发现自己在控制器中进行操作angular.element或使用$element,您可能应该改用指令。再一次,我们程序员必须处理阻止我们“理想地”工作的非理想约束(时间、金钱等),因此考虑到您的上下文,这可能是一个不错的解决方案。

如果我的任何解释没有意义,请告诉我。

原来的

http://plnkr.co/edit/vYTdxPwgwqZSgK5m9yk9?p=preview

这是你想要的吗?

JavaScript

  $scope.items = [{
    id: 1,
    show: false,
    name: 'test1'
  }, {
    id: 2,
    show: true,
    name: 'test2'
  }, {
    id: 3,
    show: true,
    name: 'test3'
  }];

  $scope.selections = [1, 2];

  $scope.getStyleForIndex = function (index) {
    var item;
    for (var i = 0; i < $scope.items.length; i++) {
      if (i === index) {
        item = $scope.items[i];
        break;
      }
    }

    return item.show ? { color: "red" } : { color: "blue" };
  }

  $scope.select2options = {
    allowClear: true,
    formatResult: function(item, container) {
      var color = $scope.getStyleForIndex(parseInt(item.id, 10)).color;
      container.html('<span style="color:' + color  + '">RESULT ' + item.text + '</span>');
    },
    formatSelection: function(item, container) {
      container.append($compile('<span ng-style="getStyleForIndex(' + item.id + ')">SELECTION ' + item.text + '</span>')($scope));
    }
  }

HTML

  <div ng-repeat="item in items">
    {{ item.name }}  
    <input type="checkbox" ng-model="item.show" />
  </div>

  <select ui-select2="select2options" ng-model="selections" style="width:200px" multiple="true" ng-options="i.id as i.name for i in items"></select>
  {{selections}}
于 2014-08-31T07:32:35.743 回答