服务无法注入控制器

Service failed to inject into controller

本文关键字:控制器 注入 服务      更新时间:2023-09-26

我收到此错误

参数"AppCtrl"不是函数,得到字符串

我的应用程序.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));
        });
    }
}

不知道我哪里出错了,我已经作为参数新鲜按下到 AppCtrl 函数中。

你必须改变行

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

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

而且控制器功能应该是这样的

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