【问题标题】:Service failed to inject into controller服务注入控制器失败
【发布时间】:2014-11-24 12:51:19
【问题描述】:

我收到了这个错误

参数 '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 函数中。

【问题讨论】:

    标签: javascript angularjs


    【解决方案1】:

    你必须换行

    来自

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

    收件人

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

    还有控制器功能应该是这样的

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

    【讨论】:

    • 哎呀,我搞砸了。但是参数的顺序在控制器功能中真的很重要吗?
    • 否,但使用更多参数时应该很容易处理:)
    • 看看这个第一个答案会很有用stackoverflow.com/questions/19238191/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-23
    • 2016-01-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多