Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

Looking the things that have ngRepeat, I can't figure in what scope is the value that return this function

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

//This input is part of a 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!

like image 994
EPotignano Avatar asked Oct 02 '13 03:10

EPotignano


People also ask

What are the directives of AngularJS?

AngularJS DirectivesThe ng-app directive initializes an AngularJS application. The ng-init directive initializes application data. The ng-model directive binds the value of HTML controls (input, select, textarea) to application data. Read about all AngularJS directives in our AngularJS directive reference.

Which directive can be used to write AngularJS expressions?

The ng-app directive is used to initialize the AngularJS application.

How does expression work in AngularJS with example?

AngularJS expressions can be written inside double braces: {{ expression }} . AngularJS expressions can also be written inside a directive: ng-bind="expression" . AngularJS will resolve the expression, and return the result exactly where the expression is written.

What is $$ in AngularJS?

The above output will be produced when we add $$, since, it acts as private object. So, to prevent accidental name collisions while writing your code angular prefixes public objects with $ and private objects with $$. Like 1.


1 Answers

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)
        })
    }
}

})

like image 140
ops.rio Avatar answered Oct 21 '22 16:10

ops.rio