I am new to Angular JS. Here is my html :
<!DOCTYPE html>
<html>
<head>
<title>My App</title>
<script src = "angular.js"></script>
</head>
<body ng-app="myApp">
<div ng-controller="index">
{{message}}
</div>
<form>
First Name:<br>
<input type="text" ng-model="user.username">{{user.username}}<br>
Last Name:<br>
<input type="password" ng-model="user.password">
</form>
<script src = "app.js"></script>
<script src = "credentials.js"></script>
</body>
</html>
Then I want to initialize some data for username and password :
(function(){
var app=angular.module("myApp",[]);
app.controller("index", ["$scope", function($scope){
$scope.message="Hello, it's me!";
$scope.user={
username: "admin",
password: "admin"
};
}]);
})();
The username and password doesn't get displayed. Why?
You're problem is that you have bound the controller to the div-tag surrounding the {{message}} expression and not the whole block containing the form. If bind your controller to a surrounding element youre code will work as expected.
Here's one way to do this:
<body ng-app="myApp">
<div ng-controller="index">
<div>
{{message}}
</div>
<form>
First Name:<br>
<input type="text" ng-model="user.username">{{user.username}}<br>
Last Name:<br>
<input type="password" ng-model="user.password">
</form>
</div>
</body>
Your controller div is not covering the area where you have user object. controller div must include the code in which you are accessing user object
You can change the code like this
<body ng-app="myApp">
<div ng-controller="index">
{{message}}
<form>
First Name:<br>
<input type="text" ng-model="user.username">{{user.username}}<br>
Last Name:<br>
<input type="password" ng-model="user.password">
</form>
</div>
<script src = "app.js"></script>
<script src = "credentials.js"></script>
</body>
<div ng-app="">
<p>Name: <input type="text" ng-model="name"></p>
<p ng-bind="name"></p>
</div>
as you can see in this example
ng-model directive binds the value of HTML controls (input, select,
textarea) to application data. ng-bind directive binds application
data to the HTML view.
i can see ng-model in your code but there is not ng-bind which actually binds data to your view. probably that is the reason why your code is not displaying username and password.
With the ng-model directive you can bind the value of an input field to a variable created in AngularJS.
You can use the ng-bind directive, which will bind the innerHTML of the element to the specified model property:
ng-model for input elements like input, textarea and ng-bind for h1,label etc.
<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>
<div ng-controller="index" ng-app="myApp">
<form>
First Name:
<br>
<input type="text" ng-model="user.username">
<br> Last Name:
<br>
<input type="password" ng-model="user.password">
</form>
</div>
<script>
var app = angular.module("myApp", []);
app.controller("index", ["$scope", function($scope) {
$scope.user = {
username: "admin",
password: "admin"
};
}]);
</script>
</body>
</html>
As,Venu prasad H S and domyos already suggested , a reply to the comment of M. Ko as i don't have enough reputation to comment,
You have defined the ng-model out of the scope of the controller,
<div ng-controller="index">
{{message}}
</div>
What this does is that your controller can access only the stuff inside this div only, as many others suggested you can put an entire div in the body you can do
<body ng-app="myApp">
<div ng-controller="index">
<div>
{{message}}
</div>
<form>
First Name:<br>
<input type="text" ng-model="user.username">{{user.username}}<br>
Last Name:<br>
<input type="password" ng-model="user.password">
</form>
</div>
As very aptly provided by domyos , now what this does is that your controller can get to your form and you can display the values you desire.
Alternatively you can also but your controller in the body tag with your ng-app
<body ng-app="myApp" ng-controller="index">
but it is recommended that you put it inside a div, you want to do this so that you can have multiple controllers in one module i.e. on ng-app.
Also, try to name controller as "indexCtrl" as in IndexController it is just a naming convention but that makes your code more readable.
Hope you find this information useful.
Use ng-app and ng-controller directives in your HTML page.
Here is link of Plunker for the same.
Plunker
And donot forget to give reference
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
Hi I'm trying to set required input fields when parent div is active?
Only active div's inputs must be required. I'm trying to do this for form submit. I also try with jQuery but it didn't worked.
PS: In fact, I don't want to use any jQuery code if possible.
My codes as seen below:
var app = angular.module("App", []);
app.controller("Controller", function($scope) {
$scope.aClick = function(event) {
$('input').removeAttr("ng-required");
$(event.target).attr("ng-required", false);
};
});
<!DOCTYPE html>
<html ng-app="App">
<head>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
<script src="//code.jquery.com/jquery-1.9.1.min.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body ng-controller="Controller">
<form name="frm">
<div ng-click="aClick($event)">
<input type="text" placeholder="Name" ng-model="name" ng-required="true">
<input type="text" placeholder="Surname" ng-model="surname" ng-required="true">
</div>
<div ng-click="aClick($event)">
<input type="text" placeholder="Age">
<input type="text" placeholder="Gender">
</div>
<button ng-disabled="frm.$invalid">Send</button>
</form>
</body>
</html>
Thank you!
ng-required doesn't have to be hard coded to true or false. You can use a variable off the scope. Then you can set that variable to true or false depending on what div is active or any other condition you want to account for. Maybe something like this depending on what you are doing
<div ng-click="aClick($event)">
<input type="text" placeholder="Name" ng-model="name" ng-required="div1IsActive">
<input type="text" placeholder="Surname" ng-model="surname" ng-required="div1IsActive">
</div>
$scope.aClick = function(event) {
$scope.div1IsActive = true; // or whatever
};
I'm new to angularjs and besides working on a project using angularjs I'm learning it as well. The query which I have, is relevant to the following code:-
<html ng-app>
<head>
<meta charset="utf-8">
<title>Angular.js Example</title>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.1/angular.min.js"></script>
</head>
<body>
Name:<input ng-model="name" type="text"/>
<br>
Name:<input ng-model="name" type="text"/>
<br>
Name:<input ng-model="name" type="text"/>
<br>
Hello {{name}}
</body>
</html>
In this code, there are ngModel directives with every input control and when I type in any one of the input control the value of other two input controls also update. I know ngModel directive binds input, select and textarea with property and changes in any of the control are detected in {{expression}} observing directive. But here I didn't see any {{expression}} observing directive in input controls. So, how the values in input controls are updated.
Can anybody shed some light on this?
I tried your code, it works in plunker. The "Hello ..." gets updated per character I type : http://plnkr.co/edit/U9IpmKNALfpAqNNhoTHa?p=preview
<html ng-app>
<head>
<meta charset="utf-8">
<title>Angular.js Example</title>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.1/angular.min.js"></script>
</head>
<body>
Name:
<input ng-model="name" type="text" />
<br>Name:
<input ng-model="name" type="text" />
<br>Name:
<input ng-model="name" type="text" />
<br>Hello {{name}}
</body>
</html>
This is a really simple example:
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<meta charset="utf-8" />
<script data-semver="1.2.11" src="http://code.angularjs.org/1.2.11/angular.js" data-require="angular.js#1.2.x"></script>
</head>
<body>
<form name="myForm">
<div>
<input type="number" name="number" ng-model="num" ng-pattern="/^\d+$/"/>
<br>error: {{ myForm.number.$error }}
</div>
<div>
<input type="text" name="text" ng-model="text" ng-pattern="/^\d+$/"/>
<br>error: {{ myForm.text.$error }}
</div>
</form>
<script type="text/javascript" charset="utf-8">
angular.module('myApp', []);
</script>
</body>
</html>
Here is the plunker
With both using the same ng-pattern, only the one with input type text work. Am I missing something here?
The HTML5's number type is not currently supported by AngularJS. If you put required directive in the input control, the validation will be triggered, and you can get the status from myForm.number.$valid. Hope it helps.
I'm just getting started with angularjs and noticed that forms within a ng-app directive won't submit. Any ideas of how I can make the form submit?
<!DOCTYPE html>
<html ng-app>
<!--<![endif]-->
<head>
<meta charset="utf-8" />
</head>
<body>
<form>
<input type="text" name="foo" />
<input type="submit" value="Submit" />
</form>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
</body>
</html>
According to the documentation :
Angular prevents the default action (form submission to the server) unless the <form> element has an action attribute specified.