2

this question may be a very very basic question of 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.

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.

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-

<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-

  1. What is the way of giving my angular app a name?

  2. Why in my case it is not working?

Thanks in advance for helping.

3
  • you must define app for your view. Commented Mar 25, 2016 at 8:38
  • Can u please give me a detailed answer, please @hadiJZ Commented Mar 25, 2016 at 8:39
  • Take a look at this answer, it's explain few things about modules and controllers definitions stackoverflow.com/a/26797874/930170 Commented Mar 25, 2016 at 8:43

2 Answers 2

3

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>

Sign up to request clarification or add additional context in comments.

3 Comments

So, if I define a app name, it should be defined in JS file, right?
it is better if you defined in js file. but you can define it in script tag in html.
I am not asking that, is there any way of doing that in HTML all like other elements done in this code?
0

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.)

For example: Name: The ng-app directive tells AngularJS that the <div> element is the "owner" of an AngularJS application. In the sample code mentioned, angularJS is configured to work in default mode. Hence ng-app is optional, i.e is null/empty.

However if you wish to bootstrap your app/HTMl with ng-app="my_try_app", then can follow any of below

  1. Define a angular controller either inside script tag or separate Js file & import using <script src> ,

    For eg. add below inside your HTML file

    <script>
    var app = angular.module('my_try_app', []);
    app.controller('myTryCtrl', function($scope) {
    $scope.text= "[email protected]";    
    });
    </script>
    
  2. For larger apps, i,e with multiple views, bootstrap angular using.. angular.bootstrap(document,["my_try_app"]);

Hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.