0

I can't figure out why i can't bind to a select element there is my code:

<select ng-model="site" ng-change="getAll()">
  <option value="SG1">SG1</option>
  <option value="PZ1">PZ1</option>
  <option value="NE1">NE1</option>
</select>

getAll() make an alert of 'site' but the var is never updated. $scope.site is nerver use except in getAll()

  $scope.getAll = function () {
    alert($scope.site);
  }

If i set $scope.site to a value it is display but never update either

Edit: I forgot a big detail... The select is display with a ng-include directive

<section id="sectionLeft" ng-include="nav[navId]">

</section>
Sandroggy
  • 97
  • 2
  • 9

2 Answers2

1

ng-include creates a new scope which prototypally inherits from your controller. So you are initially reading the selected option from your controller, but when the select element writes a new selected option it ends up writing to the inherited scope.

You can bind to an object instead.

Controller:

    $scope.data = { site: "SG1" };
    $scope.getAll = function() {
        alert($scope.data.site);
    }

Template:

    <select ng-model="data.site" ng-change="getAll()">
        <option value="SG1">SG1</option>
        <option value="PZ1">PZ1</option>
        <option value="NE1">NE1</option>
    </select>

See this answer for more details.

If you don't like switching to an object, look up controller as syntax and bind directly to the controller instead of $scope.

Frank Modica
  • 10,238
  • 3
  • 23
  • 39
0

I've fiddled your code and i'm able to get the updated value in the alert box.

$scope.getAll = function() {
    alert($scope.site);
};

Working Fiddle

Arunkumar G
  • 126
  • 6