简体   繁体   English

在为ng-app提供valule之后,AngularJS功能失败

[英]AngularJS functionality fail after given a valule to ng-app

I have below code ( jsfiddle code sample ) 我有下面的代码( jsfiddle代码示例

<div ng-app="app">

   <p>Input something in the input box:</p>
   <p>Name: <input type="text" ng-model="name"></p>
   <p ng-bind="name"></p>
   <p>{{name}}</p>

</div>

this is not working when i use <div ng-app="app"> but it is working fine when I use <div ng-app=""> (ie when remove the value of ng-app ) 当我使用<div ng-app="app">这不起作用,但是当我使用<div ng-app=""> (即删除ng-app的值时)它工作正常

why is that happening ? 为什么会这样?

You might need to initialise angular module as follows: 您可能需要初始化角度模块,如下所示:

angular.module("app", []);

and it will work all fine! 它会工作得很好!

Here is documentation of angular.module 是angular.module的文档

The angular.module is a global place for creating, registering and retrieving Angular modules. angular.module是一个创建,注册和检索Angular模块的全局场所。 All modules (angular core or 3rd party) that should be available to an application must be registered using this mechanism. 必须使用此机制注册应用程序可用的所有模块(角度核心或第三方)。

Since app is the new module you have created you need to register it in js as 由于app是您创建的新模块,因此您需要在js中注册它

angular.module("app", []);

Now ng-app is used to auto-bootstrap the angular application so if you don't have a name for it you don't need to register thats why ng-app=" " works fine but not ng-app="app" 现在ng-app用于自动引导角度应用程序,所以如果你没有它的名字,你不需要注册为什么ng-app=" "工作正常但不是ng-app="app"

The reason is you need the module app when you declare it like 原因是你声明它时需要模块app

<div ng-app="app">

You need 你需要

angular.module('app', [])

Updated fiddle . 更新了小提琴

Answer to your comment: 回答你的评论:

You can specify an AngularJS module to be used as the root module for the application. 您可以指定AngularJS模块以用作应用程序的根模块。 This module will be loaded into the $injector when the application is bootstrapped. 当应用程序被引导时,该模块将被加载到$ injector中。 It should contain the application code needed or have dependencies on other modules that will contain the code. 它应该包含所需的应用程序代码,或者依赖于包含代码的其他模块。 See angular.module for more information. 有关更多信息,请参阅angular.module。

This is quoted from the docs . 这是从文档中引用的。

When you write ng-app="app", it will search for a module with the name app. 当您编写ng-app =“app”时,它将搜索名为app的模块。 You need to initialise the module as angular.module('app', []) - then only it will work. 您需要将模块初始化为angular.module('app',[]) - 然后才能使用它。

Whenver you writing <body ng-app="myApp"> which means you are providing value to directive and angular always use to find values with directive . 当您编写<body ng-app="myApp"> ,这意味着您为指令和角度提供值始终用于查找带有指令的值。 And if you have provided value with module which means you have to inject it. 如果你已经提供了模块的价值,这意味着你必须注入它。 using 运用

angular.module('app',[]);

'[]' square brackets always contains dependencies ,if your application depends upon anyother angular application then you can inject its dependency here Follow Docs for more info: https://docs.angularjs.org/api/ng/directive/ngApp '[]'方括号总是包含依赖项,如果你的应用程序依赖于任何其他角度应用程序,那么你可以在这里注入它的依赖关系了解更多信息: https//docs.angularjs.org/api/ng/directive/ngApp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM