如何从angularjs中选择的ng-change调用服务方法?

时间:2023-01-24 11:14:51

I am new to angular js. I am trying to call factory service method 'getScoreData' from ng-change of select, but not able to get it done. please help.

我对有棱角的js很陌生。我正在尝试从ng-change的select中调用工厂服务方法“getScoreData”,但是无法完成。请帮助。

Html code:

Html代码:

<select ng-model="Score" ng-change="getScoreData(Score)" ng-options="c.name for c in     Scores"></select>

Angularjs code:

Angularjs代码:

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

app.controller('audiLayoutCtrl', function ($scope, ScoreDataService) {
    ScoreDataService.getScoreData($scope.Score, function (data) {
        $scope.ScoreData = data;
    });
});

app.factory('ScoreDataService', function ($http) {
    return {
        getScoreData: function (Score, callback) {
            var params = {
                questionCode: Score.code
            }

            return $http({
                url: 'Home/GetAvgData',
                method: 'GET',
                params: params
            }).success(callback);
        }
    };
});

above is the service factory method and its instantiate from controller. I tried instantiating from ng-change of select but its neither giving error nor its getting called.

上面是服务工厂方法及其从控制器实例化。我尝试从ng-change的select实例化,但它既不给出错误,也不调用它。

1 个解决方案

#1


56  

You have at least two issues in your code:

您的代码中至少有两个问题:

  • ng-change="getScoreData(Score)

    ng-change = " getScoreData(分数)

    Angular doesn't see getScoreData method that refers to defined service

    angle没有看到getScoreData方法,该方法引用已定义的服务

  • getScoreData: function (Score, callback)

    getScoreData:函数(得分,回调)

    We don't need to use callback since GET returns promise. Use then instead.

    我们不需要使用回调,因为GET return promise。使用之后。

Here is a working example (I used random address only for simulation):

这里有一个工作示例(我只在模拟中使用了随机地址):

HTML

HTML

<select ng-model="score"
        ng-change="getScoreData(score)" 
        ng-options="score as score.name for score in  scores"></select>
    <pre>{{ScoreData|json}}</pre> 

JS

JS

var fessmodule = angular.module('myModule', ['ngResource']);

fessmodule.controller('fessCntrl', function($scope, ScoreDataService) {

    $scope.scores = [{
        name: 'Bukit Batok Street 1',
        URL: 'http://maps.googleapis.com/maps/api/geocode/json?address=Singapore, SG, Singapore, 153 Bukit Batok Street 1&sensor=true'
    }, {
        name: 'London 8',
        URL: 'http://maps.googleapis.com/maps/api/geocode/json?address=Singapore, SG, Singapore, London 8&sensor=true'
    }];

    $scope.getScoreData = function(score) {
        ScoreDataService.getScoreData(score).then(function(result) {
            $scope.ScoreData = result;
        }, function(result) {
            alert("Error: No data returned");
        });
    };

});

fessmodule.$inject = ['$scope', 'ScoreDataService'];

fessmodule.factory('ScoreDataService', ['$http', '$q', function($http) {

    var factory = {
        getScoreData: function(score) {
            console.log(score);
            var data = $http({
                method: 'GET',
                url: score.URL
            });


            return data;
        }
    }
    return factory;
}]);

Demo Fiddle

演示小提琴

#1


56  

You have at least two issues in your code:

您的代码中至少有两个问题:

  • ng-change="getScoreData(Score)

    ng-change = " getScoreData(分数)

    Angular doesn't see getScoreData method that refers to defined service

    angle没有看到getScoreData方法,该方法引用已定义的服务

  • getScoreData: function (Score, callback)

    getScoreData:函数(得分,回调)

    We don't need to use callback since GET returns promise. Use then instead.

    我们不需要使用回调,因为GET return promise。使用之后。

Here is a working example (I used random address only for simulation):

这里有一个工作示例(我只在模拟中使用了随机地址):

HTML

HTML

<select ng-model="score"
        ng-change="getScoreData(score)" 
        ng-options="score as score.name for score in  scores"></select>
    <pre>{{ScoreData|json}}</pre> 

JS

JS

var fessmodule = angular.module('myModule', ['ngResource']);

fessmodule.controller('fessCntrl', function($scope, ScoreDataService) {

    $scope.scores = [{
        name: 'Bukit Batok Street 1',
        URL: 'http://maps.googleapis.com/maps/api/geocode/json?address=Singapore, SG, Singapore, 153 Bukit Batok Street 1&sensor=true'
    }, {
        name: 'London 8',
        URL: 'http://maps.googleapis.com/maps/api/geocode/json?address=Singapore, SG, Singapore, London 8&sensor=true'
    }];

    $scope.getScoreData = function(score) {
        ScoreDataService.getScoreData(score).then(function(result) {
            $scope.ScoreData = result;
        }, function(result) {
            alert("Error: No data returned");
        });
    };

});

fessmodule.$inject = ['$scope', 'ScoreDataService'];

fessmodule.factory('ScoreDataService', ['$http', '$q', function($http) {

    var factory = {
        getScoreData: function(score) {
            console.log(score);
            var data = $http({
                method: 'GET',
                url: score.URL
            });


            return data;
        }
    }
    return factory;
}]);

Demo Fiddle

演示小提琴