-1

I know this must be very simple but as a beginner I'am facing this issue. I have tried various solutions provided by others but was unable to solve this problem. How do I pass the value of the variable 'lang' to the controller.

Code:

<body ng-controller = "MyCtrl">
<select ng-model="lang">
<option value="dutch">Dutch</option>
<option value="english">English</option>
</select>
</body>

Controller:

app.controller('MyCtrl',[$scope,function($scope){
console.log($scope.lang);
}]);
Jayesh Nayak
  • 159
  • 1
  • 1
  • 14
  • I don't need to pass it, it is 2 ways data binding. Which mean change in one place will also changed thing in other places. ---- Also your codes seem legit. I don't know what is wrong it with? Do you have any error output in your console? Maybe providing full code how you init you angular app would be better for us to help you solve the problem. – Linh Pham Feb 19 '16 at 08:47
  • it works the way you did it: http://jsfiddle.net/df5r07a7/ – DonJuwe Feb 19 '16 at 08:52
  • 1
    duplicate http://stackoverflow.com/questions/14552976/how-to-get-option-text-value-using-angularjs – AshBringer Feb 19 '16 at 08:54
  • @LinhPham It's the reverse that I desire. the value that I get from ng-model="lang" I want to use that value in the controller to pass it to another function. – Jayesh Nayak Feb 19 '16 at 08:59

2 Answers2

1

When you selected option, $scope.lang will change already. (init- lang is undefined) But you want to fire something after lang changed, should use $watch.

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

http://jsfiddle.net/ms403Ly8/58/

with ng-init http://jsfiddle.net/ms403Ly8/60/

a.u.b
  • 1,589
  • 10
  • 25
0

html

<select ng-model="lang" ng-change="dataChange(lang)">
    <option value="dutch">Dutch</option>
    <option value="english">English</option>
</select>

js

 $scope.dataChange = function(value)
    {
        console.log(value);
    }
Naga Sandeep
  • 1,421
  • 2
  • 13
  • 26