服务无法注入控制器

时间:2014-11-24 04:42:56

标签: javascript angularjs

我收到了这个错误

  

参数' AppCtrl'不是函数,有字符串

我的app.js如下:

var App = angular.module('App', ['ionic']);

App.service("FreshlyPressed", ["$http","$log",FreshlyPressed]);
App.controller("AppCtrl", "FreshlyPressed", ["$scope", "$log", AppCtrl]);

function AppCtrl($scope, FreshlyPressed, $log){
  $scope.refresh = function(){
    FreshlyPressed.getBlogs(); 
  }
}

function FreshlyPressed($http, $log){
    this.getBlogs = function(){
        $http.jsonp("https://public-api.wordpress.com/rest/v1/freshly-pressed?callback=JSON_CALLBACK")
        .success(function(result){
            $log.info(JSON.stringify(result.posts));
        });
    }
}

不确定我哪里出错了,我已经将FreshlyPressed作为AppCtrl函数的参数。

1 个答案:

答案 0 :(得分:0)

您必须更改行

来自

App.controller("AppCtrl", "FreshlyPressed", ["$scope", "$log", AppCtrl]);

App.controller("AppCtrl", ["$scope", "$log", "FreshlyPressed", AppCtrl]);

控制器功能也应该是这样的

function AppCtrl($scope, $log, FreshlyPressed){
  $scope.refresh = function(){
    FreshlyPressed.getBlogs(); 
  }
}