将服务数据传递给控制器​​ng

时间:2016-07-07 03:35:44

标签: angularjs service controllers

当我使用getAllGenres从服务获取数据时,一切正常,但是当我尝试传递参数vm.id时,数据类型为undefined。请帮忙。附:来自Angular新人

genresService:

angular.module('movies.services', [])
.factory('genresService', ['$http', function ($http) {

    function callback () {
        // TODO: Implement callback function
    }

    function getAllGenres (callback) {
        var url = '/api/genres';

        $http.get(url)
            .success(function  (data) {
                callback(data);
            })
            .error(function  (error) {
                console.log(error);
            })
    }

    function getGenreById (id) {
        var url = '/api/genres/' + id;
        console.log(id);

        $http.get(url)
            .success(function  (data) {
                callback(data);
            })
            .error(function  (error) {
                console.log(error);
            })
    }

    return {
        getAllGenres: getAllGenres,
        getGenreById: getGenreById
    };
}]);

GenresController:

.controller('GenresController', ['$scope', 'genresService', 
            function ($scope, genresService) {
    var vm = this;

    genresService.getAllGenres(function (data) {
        vm.genres = data;
    });
}])

GenresDetailsController:

.controller('GenreDetailsController', ['$routeParams', 'genresService', 
        function ($routeParams, genresService) {
    var vm = this;
    vm.id = $routeParams.id;

    genresService.getGenreById(vm.id, function (data) {
        console.log(data);
    })
}]);

0 个答案:

没有答案