[英]Evaluating an expression in an attribute of a directive in AngularJS
I did a lot of workaround, searched and researched, but I can't figure how to achieve my goal. 我做了很多解决方法,搜索和研究,但我无法想象如何实现我的目标。
- The problem: - 问题:
-Where I get confused: - 我在哪里感到困惑:
- How I hope solve it: - 我希望如何解决它:
<input type="text" name="newComission_dateFrom" ng-model="newCommission.from" notincluded=myFunction({{$index}})/>
and the function myFunction, will iterate over a list that contains all the addedCommissionsDates and will compare it with all the ranges of dates, except with the range contained in addedCommisionsDates[index]. 函数myFunction将迭代一个包含所有addedCommissionsDates的列表,并将它与所有日期范围进行比较,除了包含在addedCommisionsDates [index]中的范围。
The objective is can evaluate an expression in an attribute without use an isolated scope. 目标是可以在不使用隔离范围的情况下评估属性中的表达式。
I had a lot of problems with isolated scopes, and I finished agreeing with this post: 孤立的范围我遇到了很多问题,我完成了对这篇文章的同意:
When writing a directive, how do I decide if a need no new scope, a new child scope, or a new isolate scope? 在编写指令时,如何确定是否不需要新范围,新子范围或新隔离范围? . 。
EDIT I was looking how was implemented ngRequire, because ngRequire can accept ng-require="aFunction()", I reached this goal in my directive, using $parsers. 编辑我正在寻找如何实现ngRequire,因为ngRequire可以接受ng-require =“aFunction()”,我在我的指令中使用$ parsers达到了这个目标。 So, I can execute a function now! 所以, 我现在可以执行一个功能了! I did some progress using this, but I want to have the result of execute the function in my directive, I want to reach something like this 我使用它做了一些进展,但我希望在我的指令中执行该函数的结果,我希望达到这样的效果
rangesToEval = //the result of my function or expression. rangesToEval = //我的函数或表达式的结果。
Looking the things that have ngRepeat, I can't figure in what scope is the value that return this function 查看具有ngRepeat的内容,我无法确定返回此函数的值的范围
if(attrs.notincluded) {
var notIncludedValidator = function(value) {
ngModelCtrl.$setValidity('notincluded', !attrs.notincluded);
return value;
};
}
all works good because I'm using a boolean, but, I want to use a list that is the result of execute the expresion in the attribute notincluded 一切运作良好,因为我使用的是一个布尔值,但是,我想用一个列表,在属性执行expresion的结果notincluded
//This input is part of a ng-repeat //此输入是ng-repeat的一部分
<input type="text" ng-model="commission.date" ng-required="true" notincluded="filterDatesFn({{$index}})" />
I have the function in my controller : 我在我的控制器中有这个功能:
$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;
}
};
So my next objective is can change to: 所以我的下一个目标是可以改为:
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;
};
}
I will be posting the progress of this research. 我将发布这项研究的进展。
I will be grateful is somebody can help or share any idea 我将很感激有人可以帮助或分享任何想法
See you later! 回头见!
have you tried pass this function like '&' parameter in your directive? 你试过在你的指令中传递像'&'参数这样的函数吗?
like that: 像那样:
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.