Domanda

Example problem http://plnkr.co/edit/nqgujfj7msKoJ4Tm3Tlz?p=preview

The universal loade xls files

steps:

  1. load xls file
  2. set select value according to col data and again load

Why , on second step function changeSelect return undefined, or how on second step get value all select's user-selected!?

È stato utile?

Soluzione

Short answer: You are asking for $scope.myOpt from a scope that is an ancestor of the scope that actually contains myOpt.

Long answer: This code:

<body ng-controller="MainCtrl">
    ...
    <tr ng-repeat="row in rows">
        <td ng-repeat="cell in row">
            <select ng-change="changeSelect();" ng-model="myOpt" ng-options="..."></select>

Creates the following scope hierarchy:

$rootScope
    MainCtrl scope <- myOpt is read here
        1st ng-repeat scope
            2nd ng-repeat scope <- myOpt is set here

According to JS prototypical inheritance, the ancestor scope cannot see the variable.

For your case I'd suggest passing myOpt to the changeSelect():

<select ng-change="changeSelect(myOpt)" ng-model="myOpt" ng-options="..."></select>

And:

$scope.changeSelect = function(x) {
    console.log(x); // or whatever
};
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top