【问题标题】:Chosen.js doesn't work well with Angular directiveChosen.js 不适用于 Angular 指令
【发布时间】:2014-09-20 21:02:00
【问题描述】:

我在这里为这个问题创建了一个 plunker 代码 http://plnkr.co/edit/mTaRPoAsQQQ6OSEQLS3a?p=preview

这是列表的 HTML 代码

<select ng-model="modSelectedState" id="inputLocation-" name="filter_location"    
     class="location" ng-options="state as state.full_name for state in states" 
     chosen="states">
<option value="">State</option>

如你所见,我创建了一个指令并将其命名为“选择”,代码如下:

app.directive('chosen', function ($http) {
var linker = function (scope, element, attr) {
    scope.$watch(scope.states, function (oldVal, newVal) {
        element.trigger('liszt:updated');
    });
    scope.$watch(attr.ngModel, function () {
        element.trigger('liszt:updated');
    });
    element.chosen({disable_search_threshold: 20});
};

return {
    restrict: 'A',
    link: linker
}
});

请注意,我使用的是旧版本的 'chosen',它需要 'liszt:updated' 而不是 'chosen:updated'

我知道状态正在被填充,因为当我检查代码时,我可以在原始 SELECT 中看到它们。有什么想法吗?

【问题讨论】:

  • 您是否注意到控制台出现错误?它说错误:[$compile:multidir] 多个指令 [ngModel, ngModel] 要求在:
  • @WebDever 是的。它与第二个指令有关。我已将其删除以避免混淆。该指令仍然无效。

标签: javascript jquery angularjs jquery-chosen


【解决方案1】:

您必须将指令上的 $watch 设置为您需要监视的范围内的变量名称。发生的事情是手表在列表有元素之前运行“触发器”。

我放了一些 $logs 以便你查看

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

scope.$watch('states', function (newVal, oldVal) {
    element.trigger('liszt:updated');
    $log.info('trigger');            
});

【讨论】:

  • 顺便说一下,请注意已经有一个选择的指令写在角度github.com/localytics/angular-chosen/blob/master/chosen.js
  • 我知道这个指令但是我不能使用它。我正在使用一个使用不同选择版本的模板。建议的版本不同,导致失去原始设计。
  • 谢谢它现在像魔术一样工作。出于一般目的,我将 'states' 更改为 'attr.chosen' 并在 HTML 中添加了 'chosen="states"'。
猜你喜欢
  • 2021-11-27
  • 1970-01-01
  • 2018-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-24
  • 2015-04-23
相关资源
最近更新 更多