角是不承认全球功能控制器 [英] Angular is not recognizing global functions as controllers

查看:114
本文介绍了角是不承认全球功能控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

采用了棱角分明的1.3.x,全局函数不被识别为控制器。什么错?如何纠正呢?

 函数myController的(){
  //等
}

 < D​​IV NG控制器=myController的>< / DIV>

控制台显示以下错误:


  

错误:[NG:AREQ]
  <一href=\"http://errors.angularjs.org/1.3.2/ng/areq?p0=MyController&p1=not%20a%20function%2C%20got%20undefined\" rel=\"nofollow\">http://errors.angularjs.org/1.3.2/ng/areq?p0=MyController&p1=not%20a%20function%2C%20got%20undefined



解决方案

根据 的文档


  

如果当前$ controllerProvider被配置为使用全局(经由
  $ controllerProvider.allowGlobals()),这也可能是一个名称
  全球范围内(不推荐)访问的构造函数。


这个变化 角抵1.3.0 beta.15(更新日志)

这是从旧版本,这将承认全球功能默认控制器角的行为的改变。这是不好的做法,不应该反正使用。具有讽刺意味的​​是,为了使用它们,你无论如何都会使用为您的应用适当的设置,而不想在这一点无论如何使用它们。

所以,你需要设置你的应用程序是这样的: NG-应用=对myApp NG-控制器=myController的

  angular.module('对myApp',[])
.controller('myController的',函数($范围){
    $ scope.author = {
      '名':'穆罕默德Mohabati',
      标题:网页设计,
      '公司':'MohabatiPro
    };
  })
;

allowGlobals 则可以用设置:

 的.config(函数($ controllerProvider){
  $ controllerProvider.allowGlobals();
});

NG-控制器=SomeFunction将像一个全局函数工作:

 函数SomeFunction($范围){//等

但不这样做。 :)

Using Angular 1.3.x, global functions are not recognized as controllers. What's going wrong? How do I correct this?

function MyController() {
  // etc
}

<div ng-controller="MyController"></div>

The console shows the following error:

Error: [ng:areq] http://errors.angularjs.org/1.3.2/ng/areq?p0=MyController&p1=not%20a%20function%2C%20got%20undefined

解决方案

According to the docs:

If the current $controllerProvider is configured to use globals (via $controllerProvider.allowGlobals()), this may also be the name of a globally accessible constructor function (not recommended).

This change arrived in Angular 1.3.0-beta.15 (changelog).

This is a change in Angular's behavior from older versions, which would recognize global functions as controllers by default. It's bad practice and shouldn't be used anyway. The ironic thing is that in order to use them, you would have to use a proper setup for your app anyway, and wouldn't want to use them at that point anyway.

So, you need to setup your app this way: ng-app="myApp and ng-controller="MyController"

angular.module('myApp', [])
.controller('MyController', function($scope) {
    $scope.author = {
      'name' : 'Mohammad Mohabati',
      'title' : 'Web Design',
      'company' : 'MohabatiPro'
    };
  })
;

allowGlobals could then be set with:

.config(function($controllerProvider) {
  $controllerProvider.allowGlobals();
});

so that ng-controller="SomeFunction" will work with a global function like:

function SomeFunction($scope) { //etc

but don't do it. :)

这篇关于角是不承认全球功能控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆