[英]Evaluating an expression in an attribute of a directive in AngularJS
我做了很多解決方法,搜索和研究,但我無法想象如何實現我的目標。
- 問題:
- 我在哪里感到困惑:
- 我希望如何解決它:
<input type="text" name="newComission_dateFrom" ng-model="newCommission.from" notincluded=myFunction({{$index}})/>
函數myFunction將迭代一個包含所有addedCommissionsDates的列表,並將它與所有日期范圍進行比較,除了包含在addedCommisionsDates [index]中的范圍。
目標是可以在不使用隔離范圍的情況下評估屬性中的表達式。
孤立的范圍我遇到了很多問題,我完成了對這篇文章的同意:
在編寫指令時,如何確定是否不需要新范圍,新子范圍或新隔離范圍? 。
編輯我正在尋找如何實現ngRequire,因為ngRequire可以接受ng-require =“aFunction()”,我在我的指令中使用$ parsers達到了這個目標。 所以, 我現在可以執行一個功能了! 我使用它做了一些進展,但我希望在我的指令中執行該函數的結果,我希望達到這樣的效果
rangesToEval = //我的函數或表達式的結果。
查看具有ngRepeat的內容,我無法確定返回此函數的值的范圍
if(attrs.notincluded) {
var notIncludedValidator = function(value) {
ngModelCtrl.$setValidity('notincluded', !attrs.notincluded);
return value;
};
}
一切運作良好,因為我使用的是一個布爾值,但是,我想用一個列表,在屬性執行expresion的結果notincluded
//此輸入是ng-repeat的一部分
<input type="text" ng-model="commission.date" ng-required="true" notincluded="filterDatesFn({{$index}})" />
我在我的控制器中有這個功能:
$scope.filterDatesFn = function(index) {
// in this list, we will add the dates of the defined commissions
if($scope.addedCommisionsDate) {
var listToEvalue= [];
for ( var i = 0; i < $scope.addedCommisionsDate.length; i++) {
if(i != index) {
listToEvalue.push($scope.addedCommisionsDate[i]);
}
}
return listToEvalue;
}
};
所以我的下一個目標是可以改為:
if(attrs.notincluded) {
var notIncludedValidator = function(value) {
--put the listToEvalue here and do my comparations and validate or invalidate the form in base of the comparation That I do here.
return value;
};
}
我將發布這項研究的進展。
我將很感激有人可以幫助或分享任何想法
回頭見!
你試過在你的指令中傳遞像'&'參數這樣的函數嗎?
像那樣:
App.directive('myDirective', function(){
return {
scope: {
myFc: '&'//if the name of parameter is the same in html if not use '&nameOfParameter'
},
link: function($scope, $el, $attr){
$el.bind('click', function(value){
$scope.myFc(value)
})
}
}
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.