【问题标题】:angularjs [ng:areq] Argument 'fn' is not a function, got stringangularjs [ng:areq] 参数 'fn' 不是函数,得到字符串
【发布时间】:2014-06-29 09:52:11
【问题描述】:

我是 Angular js 的新手。我遇到以下错误,请帮帮我

[ng:areq] 参数 'fn' 不是函数,得到字符串

var app = angular.module('demo',[]);


app.config('$routeProvider',function($routeProvider){

    $routeProvider.when('/add',{
        templateUrl:'demo/add.html',
        controller:'addcontroller'
    }).
    when('/order',{
        templateUrl:'demo/order.html',
        controller:'ordercontroller'
    });

});

app.controller('addcontroller',function($scope){
    $scope.message="order";
});
app.controller('ordercontroller',function($scope){
    $scope.message="order";
});

【问题讨论】:

  • 您在何时何地收到该错误?

标签: angularjs


【解决方案1】:

由于这里添加了各种用例,我想我也会添加我的。重新格式化功能服务时出现此错误

angular
.app('myApp')
.service('MyService', function (){
  // do something
return MyService;
})

进入一个类对象:

export default class MyService { ... }

angular
    .app('myApp')
    .service('MyService', MyService);

我的问题是我的服务在函数结束时返回自身,我需要添加一个类函数,它会这样做:

export default class MyService {
  constructor(){
  // doing something
  }

  get() {
  return MyService;
  }
}

这为我解决了这个问题。 :)

【讨论】:

    【解决方案2】:

    问题在于在数组之外定义工厂依赖项。很可能缩小也是一个问题。

    //Error in this
      angular
        .module('mymodule').factory('myFactory', 'thisconstant', function (thisconstant) {
    
    });
    

    这是由

    修复的
    //correct code
      angular
        .module('mymodule').factory('myFactory', ['thisconstant', function (thisconstant) {
    
    }]);
    

    【讨论】:

      【解决方案3】:

      在第一行你需要像这样使用,

      var app = angular.module('demo', ['ngRoute']);
      

      并像这样使用路由,

      $routeProvider.when('/add').then({
              templateUrl:'demo/add.html',
              controller:'addcontroller'
          });
      

      只需尝试这些步骤一次。

      【讨论】:

        【解决方案4】:

        我误解了 JavaScript 中处理匿名函数和命名函数的方式而收到此错误。

        这会导致错误:

        angular.module("app").factory("myDataService", ['$resource', myDataService]);
        var myDataService = function($resource) {
            return $resource('/url');
        }
        

        我应该这样做:

        var myDataService = function($resource) {
            return $resource('/url');
        }
        angular.module("app").factory("myDataService", ['$resource', myDataService]);
        

        或者这个:

        angular.module("app").factory("myDataService", ['$resource', myDataService]);
        function myDataService($resource) {
            return $resource('/url');
        }
        

        更多关于匿名函数和命名函数的区别here

        【讨论】:

          【解决方案5】:

          我知道这是一篇较旧的帖子,但我想我会用这个确切的错误来贡献我的代码有什么问题。我以这种方式将服务注入到我的控制器中:

          theApp.directive('theDirective', 'myService', ['$http', function ($http, myService) {}]);
          

          而不是这个:

          theApp.directive('theDirective', ['$http', 'myService', function ($http, myService) {}]);
          

          请注意,我的服务包含在内联数组注释的之外!对我来说,这是一个愚蠢的举动,花费了我太多时间。

          【讨论】:

            【解决方案6】:

            虽然与此问题的上下文没有直接关系,但此错误消息也可能是由返回函数以外的解析块引起的,如下所示:

            $stateProvider.state('mystate', {
                url: "/myurl",
                templateUrl: "mytemplate.html",
                controller: "MyController",
                resolve: {
                    authenticatedUser: securityAuthorizationProvider.requireAuthenticatedUser
                }
            });
            

            如果securityAuthorizationProvider.requireAuthenticatedUser 恰好是未定义的,你会得到这个错误。

            【讨论】:

              【解决方案7】:

              我认为错误在配置块中,应该是:

              app.config(function($routeProvider){
                // routeProvider config
              });
              

              或更好:

              app.config(['$routeProvider', function($routeProvider){
                // routeProvider config, allows minification
              }]);
              

              注释是为了使缩小正常工作。您可以在 AngularJS 文档https://docs.angularjs.org/tutorial/step_05 上阅读更多相关信息 请注意,这种做法需要在整个应用程序中进行才能正常工作。

              【讨论】:

                猜你喜欢
                • 2015-05-10
                • 2016-11-30
                • 1970-01-01
                • 2015-10-20
                • 1970-01-01
                • 2013-06-17
                • 1970-01-01
                • 1970-01-01
                • 2013-04-15
                相关资源
                最近更新 更多