我想寫一個指令,它接受一個範圍變量名稱併爲其分配一個不同的命名參數傳遞給一個函數的結果。下面,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