我正在尝试使用multiple
指令基于某个属性的值将ui-select
属性添加到ng-attr-
指令。不幸的是,那不适合我。我已经设置了一个示例来展示正在发生的事情。
答案 0 :(得分:6)
我在阅读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>
正如您所看到的,两个模板只有一个指令不同:&#39; multiple&#39;。也许有更好的解决方案。
我甚至无法理解为什么ng-attr-multiple方法不起作用。
此外我已经意识到,有两个单独的输入字段通过ng-attr-multiple方法呈现。
单选案例似乎被打破了(通过删除多指令) - 这也是你的初始Plnkr。
在这里查看工作的Plnkr:http://plnkr.co/edit/T9e5tcAkcQLsDV3plfEl?p=preview
答案 1 :(得分:3)
这是你想要实现的目标:
<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>