【发布时间】:2015-10-30 13:46:31
【问题描述】:
我正在尝试根据使用ng-attr- 指令的某个属性的值将multiple 属性添加到ui-select 指令。不幸的是,这对我不起作用。我已经设置了一个 plunker 示例来展示正在发生的事情。
【问题讨论】:
标签: javascript angularjs angularjs-directive ui-select
我正在尝试根据使用ng-attr- 指令的某个属性的值将multiple 属性添加到ui-select 指令。不幸的是,这对我不起作用。我已经设置了一个 plunker 示例来展示正在发生的事情。
【问题讨论】:
标签: javascript angularjs angularjs-directive ui-select
在阅读了 Angular Repo 中提到的GitHub Issue 后,我终于明白了。
您需要设置一个具有更高priority 和terminal 属性设置为true 的指令(在我们的指令编译后跳过所有其他指令的编译)。
然后在postLink 函数中,我们将编译整个元素本身。但在此之前,我们自己的指令需要被移除(无限循环!)。
对Add directives from directive in AngularJS的大手笔
angular.module('app')
.directive('multiSelectChecker', function ($compile) {
return {
restrict: 'A',
replace: false,
terminal: true, //terminal means: compile this directive only
priority: 50000, //priority means: the higher the priority, the "firster" the directive will be compiled
compile: function compile(element, attrs) {
element.removeAttr("multi-select-checker"); //remove the attribute to avoid indefinite loop
element.removeAttr("data-multi-select-checker"); //also remove the same attribute with data- prefix in case users specify data-multi-select-checker in the html
return {
pre: function preLink(scope, iElement, iAttrs, controller) { },
post: function postLink(scope, iElement, iAttrs, controller) {
if(scope.options.Multiple == true) {
iElement[0].setAttribute('multiple',''); //set the multiple directive, doing it the JS way, not jqLite way.
}
$compile(iElement)(scope);
}
};
}
};
});
<ui-select ng-model="model.choice" multi-select-checker>
<ui-select-match>{{$item.Title}}</ui-select-match>
<ui-select-choices repeat="item.Id as item in options.SuggestedValues | filter: { Title: $select.search }">
<div ng-bind="item.Title | highlight: $select.search"></div>
</ui-select-choices>
</ui-select>
http://plnkr.co/edit/N11hjOFaEkFUoIyeWqzc?p=preview
我做了以下事情:
multi-select-checker 的包装指令
options.Multiple 是真还是假app.directive('multiSelectChecker', function() {
return {
template: '<ng-include src="getTemplateUrl()"/>',
controller: function($scope) {
$scope.getTemplateUrl = function() {
if($scope.options.Multiple == true) {
console.log("multi-select");
return "multi-select.tpl.html"
}
else {
console.log("single select");
return "single-select.tpl.html"
}
}
}
}
})
<body ng-controller="DemoCtrl">
<multi-select-checker>
</multi-select-checker>
</body>
<ui-select ng-model="model.choice">
<ui-select-match>{{$item.Title}}</ui-select-match>
<ui-select-choices repeat="item.Id as item in options.SuggestedValues | filter: { Title: $select.search }">
<div ng-bind="item.Title | highlight: $select.search"></div>
</ui-select-choices>
</ui-select>
<ui-select ng-model="model.choice" multiple>
<ui-select-match>{{$item.Title}}</ui-select-match>
<ui-select-choices repeat="item.Id as item in options.SuggestedValues | filter: { Title: $select.search }">
<div ng-bind="item.Title | highlight: $select.search"></div>
</ui-select-choices>
</ui-select>
如您所见,这两个模板只有一个指令不同:'multiple'。也许有更好的解决方案。
我什至不明白,为什么 ng-attr-multiple 方法不起作用。
此外,我已经意识到,有两个单独的输入字段通过 ng-attr-multiple 方法呈现。
而且单选案例似乎被打破了(通过删除多个指令) - 这也在你最初的 Plnkr 中。
在此处查看正在运行的 Plnkr:http://plnkr.co/edit/T9e5tcAkcQLsDV3plfEl?p=preview
【讨论】:
ng-attr 的原因。无论如何谢谢:)
ui-select 的孩子 -> 这会减少您的重复代码。
$parent。谢谢!
这是你想要达到的目标吗:
<body ng-controller="DemoCtrl">
This works perfectly well:
<ui-select ng-model="model.choice" multiple>
<ui-select-match>{{$item.Title}}</ui-select-match>
<ui-select-choices repeat="item.Id as item in options.SuggestedValues | filter: { Title: $select.search }">
<div ng-bind="item.Title | highlight: $select.search"></div>
</ui-select-choices>
</ui-select>
<br />
<br />
This does not work:
<ui-select ng-model="model.choice2" multiple="{{options.Multiple}}">
<ui-select-match>{{$item.Title}}</ui-select-match>
<ui-select-choices repeat="item.Id as item in options.SuggestedValues | filter: { Title: $select.search }">
<div ng-bind="item.Title | highlight: $select.search"></div>
</ui-select-choices>
</ui-select>
</body>
【讨论】: