0

I want to save the selected value and pop up the selected value . But when I add the ng- change directive the select stops functioning. If you remove the ng change the droplist is displayed. Help me out.

This is my html

<div ng-app="HelloApp">
  <div ng-controller="MyCtrl">
    <p>Term</p>
    <select ng-model="bob" ng-change="test()" ng-options="x for x in nos">

    </select>
  </div>
</div>

And this is my js script

**var app = angular.module('HelloApp', []);  
app.controller('MyCtrl', function($scope) {
console.log('ctrl working');
    $scope.test = function () {
     nos=["5", "10", "15"];**
     var kill=$scope.bob;
     alert ("changed!"+ kill); 
    }

});

I am new to angular and internet searches only land to complex arrays not a simple one. Please help

georgeawg
  • 48,608
  • 13
  • 72
  • 95
Tedsville
  • 21
  • 1
  • 5

3 Answers3

1

Change your script to the following :

var app = angular.module('HelloApp', []);  
app.controller('MyCtrl', function($scope) {
console.log('ctrl working');
$scope.nos=["5", "10", "15"];
    $scope.test = function () {

       var kill=$scope.bob;
       alert ("changed!"+ kill); 
    }

});

Things to note :

-The nos must be declared into the controller scope , previously it was not visible to the view. -It should be out of the test function.

Rambler
  • 4,994
  • 2
  • 20
  • 27
1

See this - fiddle

$scope.nos=["5", "10", "15"];
$scope.bob="";
$scope.test = function () {
   alert($scope.bob);

}
Disha
  • 822
  • 1
  • 10
  • 39
0

Just a Bit change , nothing major

app.controller('MyCtrl', function($scope) {
$scope.nos=["5", "10", "15"];
    $scope.test = function (x) {
     alert (x); 
    }
});

HTML

<select ng-model="bob" ng-change="test(bob)" ng-options="x for x in nos">

Thanks

Ujjwal kaushik
  • 1,618
  • 11
  • 17