在脚本2中未定义$ scope

时间:2019-03-01 13:36:45

标签: angularjs angularjs-directive angularjs-scope angularjs-controller angularjs-ng-change

无法从下拉列表中获取ng-change选择的值。

我正尝试将下拉列表中的所选值传递给脚本2中的API调用,如下所示,但无法这样做。

在chrome开发人员工具中,脚本2($ scope.changedValue = function())中显示错误消息为“未定义$ scope”。

  <!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);
                });           
        });

//脚本:2-获取选定的值并将其传递给API以获取数据。

               // 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>

看起来它本身并没有输入脚本2(未在脚本2中命中警报msg)。

任何人都可以在上述问题上为我提供帮助。

3 个答案:

答案 0 :(得分:0)

这里的问题是您在两个地方都有flv的值,似乎在ng-model="flv"内是flvng-options

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

flv中的flavor的值更改为ng-options应该可以解决问题

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

答案 1 :(得分:0)

undefined

答案 2 :(得分:0)

使用以下代码。

  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);
                    });
                }
            });

关闭括号是个问题。希望它能工作。