角度选择2格式结果未更新模板



我有一个带有以下标记的 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、布尔 show 属性和 name 属性的数组(

并选择 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 .看起来格式结果在第一次打开 select2 之前只正确更新一次结果。

Edit

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>'
      }
    }
  }

关键部分是从 jqLite 元素中获取$scope数据,然后调用 $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}}

最新更新