javascript - AngularJS - watch is not working on variable -


this code not working , can't tell why.

html

<select id="provincia" class="form-control" ng-model="ciudades" ng-options="provincia (provincia, ciudades) in provincias">  <option ng-model="valor_ciudad" value=''>elegir</option> </select> 

js

    $scope.$watch('ciudades', function(newval, oldval){         debugger;         if (newval){             $scope.prov = newval;         }     }); 

what want do? trying value picked user because variable ciudades has array of values.

$scope.provincias = {florida:['miami', 'orlando']} 

if user picks florida, $scope.ciudades ['miami', 'orlando']. need know if user choose florida or city. (.watch didn't work, not stopping on debugger)

did make myself clear?

you can use 'florida' key key , value , set prov directly select's ng-model then, listen prov changes , set ciudades based on chosen key:

var app = angular.module('app', []);  app.controller('appctrl', function($scope, $compile, $timeout) {     $scope.provincias = {      florida: ['miami', 'orlando']     };    $scope.$watch('prov', function(newval, oldval){      $scope.ciudades = $scope.provincias[newval];    });  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>    <div ng-app="app" ng-controller="appctrl">    <select ng-model="prov" ng-options="key key (key, value) in provincias">      <option ng-model="valor_ciudad" value=''>elegir</option>    </select>    <br>    $scope.prov: {{prov}}<br>    $scope.ciudades: {{ciudades}}<br>  </div>


Comments