[英]AngularJS - ng-app not working if not emplty
this question may be a very very basic question of Angular.JS .这个问题可能是一个非常非常基础的Angular.JS问题。 May be nearly this question is asked in StackOverFlow before, but I am unable to find the question, so I have to ask this question.
可能之前在StackOverFlow中几乎问过这个问题,但我找不到问题,所以我不得不问这个问题。
I am pretty new in Angular.JS and I am trying to create a basic form with Angular.JS which will validate an email address from given input.我是Angular.JS 的新手,我正在尝试使用 Angular.JS 创建一个基本表单,该表单将从给定的输入中验证电子邮件地址。
What is working for me is-对我有用的是-
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> <form ng-app="" name="myForm"> Email: <input type="email" name="myAddress" ng-model="text"> <span ng-show="myForm.myAddress.$error.email">Not a valid e-mail address</span> </form> <p>Enter your e-mail address in the input field. AngularJS will display an errormessage if the address is not an e-mail.</p>
But what I am trying to give a name of my angular app like this- ng-app="my_try_app"
so my code has been like this-但是我试图给我的
ng-app="my_try_app"
应用程序命名是这样的 - ng-app="my_try_app"
所以我的代码是这样的 -
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> <form ng-app="my_try_app" name="myForm"> Email: <input type="email" name="myAddress" ng-model="text"> <span ng-show="myForm.myAddress.$error.email">Not a valid e-mail address</span> </form> <p>Enter your e-mail address in the input field. AngularJS will display an errormessage if the address is not an e-mail.</p>
And u are seeing it is not working.你看到它不起作用。
So, my question is-所以,我的问题是——
What is the way of giving my angular app a name?给我的 angular 应用命名的方法是什么?
Why in my case it is not working?为什么在我的情况下它不起作用?
Thanks in advance for helping.提前感谢您的帮助。
you must define app for your view.您必须为您的视图定义应用程序。 like this.
像这样。
angular.module("my_try_app",[]);
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> <form ng-app="my_try_app" name="myForm"> Email: <input type="email" name="myAddress" ng-model="text"> <span ng-show="myForm.myAddress.$error.email">Not a valid e-mail address</span> </form> <p>Enter your e-mail address in the input field. AngularJS will display an errormessage if the address is not an e-mail.</p>
Please have a look at angulaJS doc below: https://docs.angularjs.org/api/ng/directive/ngApp Basically, "ng-app" is used during Bootstrap Phase, It helps the angular to initializes your module.(Determines the boundary of HTML code which should be coming under angular influence.)请查看下面的 angulaJS 文档: https ://docs.angularjs.org/api/ng/directive/ngApp 基本上,“ng-app”在引导阶段使用,它有助于 angular 初始化您的模块。(确定应该受到角度影响的 HTML 代码的边界。)
For example: Name: The ng-app directive tells AngularJS that the <div>
element is the "owner" of an AngularJS application.例如: Name: ng-app 指令告诉 AngularJS
<div>
元素是 AngularJS 应用程序的“所有者”。 In the sample code mentioned, angularJS is configured to work in default mode.在提到的示例代码中,angularJS 被配置为在默认模式下工作。 Hence ng-app is optional, ie is null/empty.
因此 ng-app 是可选的,即 null/empty。
However if you wish to bootstrap your app/HTMl with ng-app="my_try_app", then can follow any of below但是,如果您希望使用 ng-app="my_try_app" 引导您的应用程序/HTMl,则可以按照以下任一操作
Define a angular controller either inside script tag or separate Js file & import using <script src>
,在脚本标签内或单独的 Js 文件中定义一个角度控制器并使用
<script src>
导入,
For eg.例如。 add below inside your HTML file
在您的 HTML 文件中添加以下内容
<script> var app = angular.module('my_try_app', []); app.controller('myTryCtrl', function($scope) { $scope.text= "a@atcom"; }); </script>
For larger apps, i,e with multiple views, bootstrap angular using.. angular.bootstrap(document,["my_try_app"]);
对于较大的应用程序,即具有多个视图,使用 ..
angular.bootstrap(document,["my_try_app"]);
Hope this helps.希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.