Вопрос

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