无法参数传递给角指令 [英] unable to pass parameter to angular directive
问题描述
欲发起第三方控制(引导选择)之后的集合已被分配给一个源变量。对于我使用的指令,看这样的集合。
I want to initiate a third party control (bootstrap-select) after a collection has been assigned to a source variable. For that I am using a directive and watching a collection like this.
angular
.module('app').directive('bootstrapDropdown', ['$timeout',
function ($timeout) {
return {
restrict: 'A',
scope: {
collectionName: '='
},
require: '?ngModel',
link: function (scope, el, attrs) {
el.selectpicker();
scope.$watchCollection(scope.collectionName, function (newVal) {
$timeout(
function () {
el.selectpicker('refresh');
}
);
});
}
};
}
]);
如果我通过集合作为字符串的名称 $ watchCollection
它工作正常。但是,我要寻找一个通用的指令,所以我通过集合的名称,如
If I pass the name of collection as string in $watchCollection
it works fine. But I am looking for a generic directive so I am passing name of collection like
<select bootstrap-dropdown collection-name="commandGroups" ng-model="vm.Job.CommandGroup" name="ddlCommandGroup">
<option value="">Select Something</option>
<option ng-repeat="cmdGroup in commandGroups" collection-name="commandGroups" value="{{cmdGroup.Id}}">{{cmdGroup.Name}}</option>
</select>
但它不工作
推荐答案
集合名称是选择元素的一个属性,不能只用scope.collectionName因为这将返回undefined观看。
您可以使用下面的行你的链接功能您可以通过集合名属性的值:
Collection-name is an attribute on the select-element and can't just be watched by using scope.collectionName since that will return undefined. You can get the value from the 'collection-name' attribute by using the following line in your link-function:
scope.collectionName = attrs.collectionName;
不知道它是否适合你,因为我没有数据,但它可能帮助你更进一步。
Not sure if it works for you since I have no data, but it probably helps you further.
这篇关于无法参数传递给角指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!