使用NG-应用没有值 [英] Using ng-app without a value

查看:126
本文介绍了使用NG-应用没有值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常情况下,你看到的角像指令NG-应用=MyModule的初始化的页面上,但只使用它是有效的 NG-应用。但是,象外之象控制器与 Mymodule中注册,是以往任何时候都有益可言只使用 NG-应用?是否有注册控制器等等。如果你不指定模块 NG-应用方式=...

Usually, you see Angular initialized on a page with a directive like ng-app="myModule", but it is valid to just use ng-app. However, as things like controllers are registered with myModule, is it ever useful at all to just use ng-app? Is there a way to register controllers etc. if you don't specify a module to ng-app="..."?

推荐答案

此前角1.3(当你可以定义在全球范围内控制器),角能够自动发现被全局定义的控制器。

Prior to Angular 1.3 (when you could define controllers on the global scope), Angular was able to automatically discover controllers that were defined globally.

由于角1.3,所有控制器都必须在模块中定义的,因此,你会得到非常有限的功能了一个 NG-应用无模块。这可能是原型有益的,但​​即使在那里,你不会得到太多。

As of Angular 1.3, all controllers have to be defined within a module, and thus, you'd get very limited functionality out of an ng-app without a module. It could potentially be beneficial for prototyping, but even there, you're not going to get much.

所以,pre-角1.3用法:

So, pre-angular 1.3 usage:

<div ng-app>
   <div ng-controller="SomeController">
       {{something}}
   </div>
</div>

您可以定义您的JavaScript这样的,它会工作:

You could define your javascript like this and it would work:

<script>
    function SomeController($scope) {
        $scope.something = "Hello";
    }
</script>

编辑:

由于在评论中提到,您仍然可以使用这种行为 $ controllerProvider.allowGlobals()。这就是说,角队一直试图从开始定义控制器,这种方式阻止我们,并且应该避免:

As mentioned in the comments, you can still enable this behavior by using $controllerProvider.allowGlobals(). That said, the Angular team has tried to deter us from defining controllers this way from the start, and should be avoided:

请注意:虽然角度可以让你在全球范围内建立控制器功能,不建议这样做。在实际应用中,你应该用你的角模块的.controller方法为您的应用[...]

NOTE: Although Angular allows you to create Controller functions in the global scope, this is not recommended. In a real application you should use the .controller method of your Angular Module for your application [...]

这篇关于使用NG-应用没有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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