简体   繁体   English

在AngularJS中评估指令属性中的表达式

[英]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: - 问题:

  • I have a the following situation, I want to avoid the user can overlap commissions dates in a contract. 我有以下情况,我想避免用户可以在合同中重叠佣金日期。 When the user add a new commission, we show a list with the added Commissions generated with a ngRepeat, this have the difficulty of the user can edit the dates. 当用户添加新佣金时,我们会显示一个列表,其中添加了使用ngRepeat生成的佣金,这样就很难用户编辑日期。 In the part of contracts, this is not a problem, because for edit a contract, you have to go to other screen and edit it, the dates can't be modified in the same view. 在合同部分中,这不是问题,因为要编辑合同,您必须转到其他屏幕并对其进行编辑,不能在同一视图中修改日期。

-Where I get confused: - 我在哪里感到困惑:

  • When I edit a commission that was added, I have to compare it with the other that what added before, so, I want to have a list, with all the dates of the commissions defined, and can say in the directive, invoicing a function that returns a list with all the dates excluding the date of the commission that I'm editing. 当我编辑一个已添加的佣金时,我必须将其与之前添加的佣金进行比较,因此,我希望有一个列表,其中定义了佣金的所有日期,并且可以在指令中说明,开具一个函数的发票返回一个列表,其中包含所有日期,不包括我正在编辑的佣金日期。

- How I hope solve it: - 我希望如何解决它:

  • I want to do something like this: 我想做这样的事情:

<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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM