$scope is not defined in script 2

All we need is an easy explanation of the problem, so here it is.

Unable to fetch ng-change selected value from the dropdown.

I’m trying to pass the selected value from the dropdown to API call in script 2 as below, but unable to do so.

In the chrome developer tools it was showing error msg as "$scope is not defined" in the script 2( $scope.changedValue = function ()).

  <!DOCTYPE html>
    <html>
    <head>
        <script src="Scripts/angular.js"></script>
        <script src="Scripts/angular-resource.js"></script>
        <script>
            var IM_Mod_app = angular.module('IM_ng_app', []);
    
            // script 1: To fetch all flrs from API call. - working as expected. IM_Mod_app.controller("IM_Ctrl", function ($scope, $http) {
            $http({
                method: 'GET',
                url: 'http://localhost:55762/api/ItemMaintenance/GetAllFlavors',
                params: { Id: 'US06' }
            }).then(function successCallback(response) {
               // alert(response.data);         
                $scope.flavours = response.data;
            }, function errorCallback(response) {
                     // alert(response);
                });           
        });
        
    

// Script : 2 – to fetch the selected value and to pass it to API to get data.

               // IM_Mod_app.controller("IM_Ctrl", function ($scope) {
        $scope.changedValue = function () {
            alert("h1111i");
                $scope.selectedvalues = $scope.flv.FLAVOR_ID;
            }.then(function successCallback(response) {
                // success on on change event - call api to get data
                alert("hi");
                $http({
                    method: 'GET',
                    url: 'http://localhost:55762/api/ItemMaintenance/GetAllFlavors',
                    params: { Id: 'US15' }
                }).then(function successCallback(response) {
                    // scuucess on fetching data from api call
                    // alert(response.data);

                    $scope.flavours = response.data;
                }, function errorCallback(response) {
                    // error on fetching data from api call
                    // alert(response);
                });
                $scope.flavours = response.data;
            }, function errorCallback(response) {
                //  error on onchange event
            });
       // });             
    </script>

</head>
    
    <body ng-app="IM_ng_app">
        <div ng-controller="IM_Ctrl">
            <select ng-model="flv"
                    ng-options="flv.FLAVOR_ID for flv in flavours"
                    ng-change="changedValue(flv)">
                <option value="">Select Flavor</option>
            </select>
           <h1>{{flv.FLAVOR_ID}}</h1>
           
        </div>  
    </body>
    </html>

Looks like it was not entering the script 2 itself(Not hitting the alert msg in script 2).

Can any body can help me in the above issue.

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

Use the below code.

  IM_Mod_app.controller("IM_Ctrl", function ($scope, $http) {
                $http({
                    method: 'GET',
                    url: 'http://localhost:55762/api/ItemMaintenance/GetAllFlavors',
                    params: { Id: 'US06' }
                }).then(function successCallback(response) {
                   // alert(response.data);         
                    $scope.flavours = response.data;
                }, function errorCallback(response) {
                         // alert(response);
                    });  
     $scope.changedValue = function () {
                   // alert("hgjhhg");
                    alert($scope.flv.FLAVOR_ID);
                    // success on on change event - call api to get data
                    $http({
                        method: 'GET',
                        url: 'http://localhost:55762/api/ItemMaintenance/GetAllFlavors',
                        params: { Id: 'US15' }
                    }).then(function successCallback(response) {
                        alert(response.data);
                        $scope.flavours = response.data;
                    }, function errorCallback(response) {
                        // alert(response);
                    });
                }
            });

Closing parentheses is the issue. Hope it will work.

Method 2

The problem over here is you have flv value at two places which is seems to be conflicting which is ng-model="flv" and flv inside ng-options.

<select ng-model="flv" ng-options="flv.FLAVOR_ID for flv in flavours" ng-change="changedValue(flv)">
    <option value="">Select Flavor</option>
</select>

Change value of flv to flavor in ng-options should solve the issue

ng-options="flavor.FLAVOR_ID for flavor in flavours"

Method 3

IM_Mod_app.controller("IM_Ctrl", function ($scope) { $scope.changedValue = function () { alert("h1111i"); $scope.selectedvalues = $scope.flv.FLAVOR_ID; }.then(function successCallback(response) { // success on on change event - call api to get data alert("hi"); $http({ method: 'GET', url: 'http://localhost:55762/api/ItemMaintenance/GetAllFlavors', params: { Id: 'US15' } }).then(function successCallback(response) { // scuucess on fetching data from api call // alert(response.data); $scope.flavours = response.data; }, function errorCallback(response) { // error on fetching data from api call // alert(response); }); $scope.flavours = response.data; }, function errorCallback(response) { // error on onchange event }); 
}); 

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply