我想写一个指令,它接受一个范围变量名称并为其分配一个不同的命名参数传递给一个函数的结果。下面,files="result"
旨在在glob
隔离范围内创建一个{{result}}
变量。 “matching
”变量的内容将在父上下文中进行评估,并分配给隔离变量“matching
”。角度指令与隔离范围,字段无法访问
该指令然后调用一个函数,最终分配给文件指向的隔离变量(result
这里)返回的数组。 {{result}}
的扩展然后可以用于例如ng-repeat
。
该指令应该是可重用的,无需更改变量名称。
这没有发生。如果我将所有内容都分配给父项,我可以使其工作,但每次都需要更改变量名称。
angular.module('j20-glob', ['api'])
/*
* usage: <glob files="result" matching="/bin/{{prefix}}*">
* {{result}}
* </glob>
* should allow another just after the first without stomping result
* <glob files="result" matching="/something">{{result}}</glob>
*/
.directive('glob', ['$parse', 'api', function($parse, $api) {
return {
priority: 99, // it needs to run after the attributes are interpolated
restrict: 'AE',
scope: {
},
link: function(scope, iElement, iAttributes) {
var indexModel = $parse(iAttributes.files);
iAttributes.$observe('matching', function(value) {
if (!value)
return;
$api.glob(value).then(function(res) {
indexModel.assign(scope, res);
// indexModel.assign(scope.$parent, res);
});
});
}
}
}
]);
Plunker /小提琴/ CodePen可能? – 2015-02-11 19:42:02