0

I have the following markup inside my directive:

<select class="form-control" 
ng-change="changeLanguage()" 
ng-model="selectedLanguage" 
ng-options='lang.key as lang.name for lang in languages'></select>
<p>{{selectedLanguage}}</p>

And my directive's controllers relevant part is this:

$scope.languages = [{
    name: "Java",
    key: "java",
}, {
    name: "C",
    key: "c",
}];

$scope.$watch("selectedLanguage", function(newValue, oldValue) {
    console.log("watch", newValue, oldValue);
});

$scope.changeLanguage = function() {
    console.log("language changed: " + $scope.selectedLanguage)
}

However, when I change it via UI, it is not reflected in the controller, but it is available in the UI, I see the selected value. I tried both $watch and onChange to see, but both of them don't work. What is wrong here?

8
  • Try to initialize selectedLanguage in your controller as $scope.selectedLanguage=""; Commented Jan 22, 2015 at 12:37
  • @Jan I did it, it did not fix it Commented Jan 22, 2015 at 12:39
  • Maybe selectedLanguage is in a different scope. Commented Jan 22, 2015 at 12:40
  • Code is perfect. Works perfectly for me. Share a plunkr or jsFiddle if possible. Commented Jan 22, 2015 at 12:41
  • It is in a directive, would that be related? Commented Jan 22, 2015 at 12:42

2 Answers 2

2

Seems to work fine for me. Here is an example of it working with the code you provided.

var app = angular.module('myApp', []);

app.controller('Ctrl', function($scope) {

  $scope.languages = [{
    name: "Java",
    key: "java",
  }, {
    name: "C",
    key: "c",
  }];

  $scope.$watch("selectedLanguage", function(newValue, oldValue) {
    console.log("watch", newValue, oldValue);
  });

  $scope.changeLanguage = function() {
    console.log("language changed: " + $scope.selectedLanguage)
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="Ctrl">
  <select class="form-control" ng-change="changeLanguage()" ng-model="selectedLanguage" ng-options='lang.key as lang.name for lang in languages'></select>
  <p>{{selectedLanguage}}</p>
</div>

Sign up to request clarification or add additional context in comments.

Comments

1

Weird, maybe problem is not there? In pluncker it works

   (function() {

    var app = angular.module('myApp', []);

        app.controller('myCtrl', ['$scope',
        function($scope) {
          $scope.languages = [{
            name: "Java",
            key: "java",
          }, {
            name: "C",
            key: "c",
          }];

          $scope.$watch("selectedLanguage", function(newValue, oldValue) {
            console.log("watch", newValue, oldValue);
          });

          $scope.changeLanguage = function() {
            console.log("language changed: " + $scope.selectedLanguage)
          }
        }
      ]);
        })()

plunker

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.