7

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!

Community
  • 1
  • 1
EPotignano
  • 111
  • 1
  • 6

1 Answers1

1

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

})

ops.rio
  • 552
  • 1
  • 6
  • 12