Here is how I created my service

app.factory('PageService', function($http) {
    return {
        getPageTemplate: function(url) {
            return $http.get(urlBuilder(url));
        }
    }
})

And here is my controller

app.controller('PageController', ['$scope', function($scope, $http, PageService) {

    $scope.changePanel = function(url) {
        PageService.getPageTemplate(url).success(function(data) {
            console.log(url);
        })  
    }

}])

changePanel will be called when ng-click fires.

This is the message am getting

TypeError: Cannot call method 'getPageTemplate' of undefined

有帮助吗?

解决方案

You're not correctly injecting your service into your controller.

Change it to

app.controller('PageController', ['$scope', '$http', 'PageService', function($scope, $http, PageService) {
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top