0

probably a rocky question but nonetheless, i'd like to understand the reason for that.

I have an input field like so:

<div>
    <label class="col-xs-2" for="selectedJdeEsu">{{"JDE.jde_wizard.esu_name" | translate}}</label>
    <input ng-model="selectedEsu" type="text/>
    <div class = "btn wizard-next btn-primary" ng-click="addCurrent(selectedEsu)">
        <i class="fa fa-plus-circle" aria-hidden="true" ></i>
        add
    </div>
    <div ng-show="esuNameErr" style="color: red">
        {{esuNameErr}}
    </div>
</div>

and angularJS like so:

    $scope.addCurrent = function(curr){
        var found = false;

        for(var i = 0 ; i < $scope.jdeEsus.length ; i++){
            if($scope.jdeEsus[i].esuName === $scope.selectedEsu){
                $scope.selectedEsuList.push($scope.jdeEsus[i]);
                $scope.selectedEsu = '';
                $scope.esuNameErr = null;
                found = true;
                break;
            }
        }

        if(found == false && $scope.selectedEsu){
            $scope.esuNameErr = $filter('translate')('JDE.jde_wizard.esu_name_err')
        }
    };

$scope.jdeEsus is an array of objects. now my problem is that while in the loop $scope.selectedEsu is undefined, while curr gets the right value, even though it comes from the same source.

I want to know why?

No Idea For Name
  • 11,411
  • 10
  • 42
  • 70

1 Answers1

1

This is linked to scope inheritance (Javascript, AngularJS)

selectedEsu is defined in a scope linked to either an ng-if, or an other Angular directive (this scope is a son of your Controller scope), but the parent Scope : the Controller one, don't know this field.

You can found more explanations in Question such as : What are the nuances of scope prototypal / prototypical inheritance in AngularJS?

Community
  • 1
  • 1
yunandtidus
  • 3,847
  • 3
  • 29
  • 42