Professional Documents
Culture Documents
{{name|uppercase}}
{{name|lowercase}}
{{name|currency}}
{{name|currency:"Rs."}}
{{name|currency:"Rs.":4}} // 4 decimal points
{{name|number}}
{{name|number:5}}
ng-include
<div ng-include="'myext.html'"></div>
<script src="js/controllers.js" ></script>
Routing :
var myapp=angular.module("myroutepp",['ngRoute']); //$routeProvider comes from ngRoute
dependancy module
myapp.config(function ($routeProvider){
$routeProvider.when('/',{
template:'Welcome user !'
})
.when('/anotherpage',{
template:'Welcome user,again !'
})
.otherwise({
redirectTo:'/'
});
});
More on Routing
var myapp=angular.module("myroutepp",['ngRoute']); //$routeProvider comes from ngRoute
dependancy module
myapp.config(function ($routeProvider){
$routeProvider.when('/',{
templateUrl:'login.html'
})
.when('/anotherpage',{
templateUrl:'pagenotfound.html'
})
.otherwise({
redirectTo:'/'
});
});
</form>
</div>
dashboard.html
<h1>This is user dashboard</h1>
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Learn Angular</title>
<script src="js/angular.min.js" ></script>
<script src="js/angular-route.js" ></script>
<script src="js/controllers.js" ></script>
</head>
<body ng-app="myroutepp">
<div ng-view="myview"></div>
</body>
</html>
redirectTo:'/error'
});
});
myapp.controller("loginctrl",function($scope,$location,$rootScope){
$scope.submit=function(){
var username=$scope.username;
var password=$scope.password;
if(username=='admin'&&password=='admin'){
$rootScope.islogin=true;
//window.location.hash="#/dashboard";//alternative
$location.path('/dashboard');
}
else{
alert("invalid credentials");
}
}
});
$http Service
var myapp=angular.module("myhttpapp",[]);
myapp.controller("myctrl",function($scope,$http){
$http.get("http://localhost:8080/public_html/dbres.json").success(function(response){
$scope.persons= response.recs;
});
});
{
"recs": [
{ "name":"vignesh",
"Age":"22"
},
{ "name":"kiran",
"Age":"25"
},
{ "name":"praveen",
"Age":"22"
},
{ "name":"naren",
"Age":"27"
},
{ "name":"swarup",
"Age":"21"
},
{ "name":"swapna",
"Age":"24"
}
]
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Learn Angular</title>
<script src="js/angular.min.js" ></script>
<script src="js/controllers.js" ></script>
</head>
<body ng-app="myhttpapp">
<div ng-controller="myctrl">
<h2>Our Employees</h2>
<ul>
<li ng-repeat="person in persons">
{{ person.name +" : "+person.Age }}
</li>
</ul>
</div>
</body>
</html>
Simple Searchbox:
<input ng-model="sugg" type="text">
<ul>
<li ng-repeat="person in persons | filter:sugg">
{{ person.name +" : "+person.Age }}
</li>
</ul>
Advanced Searchbox
<body ng-app="myhttpapp">
<div ng-controller="myctrl">
<h2>Our Employees</h2>
<div id="searchboxes">
<input ng-model="globalSearch.$" type="text">
Watch:
var myapp=angular.module("myhttpapp",[]);
myapp.controller("mycont",function($scope){
$scope.countern=0;
$scope.$watch("myinp",function(oldValue,newValue){
$scope.countern++;
});
});
<body ng-app="myhttpapp">
<div ng-controller="mycont">
<input type="text" ng-model="myinp">
<h1>{{myinp}}</h1>
<h4>{{countern}}</h4>
</div>
</body>
Digest
var myapp=angular.module("myhttpapp",[]);
myapp.controller("mycont",function($scope){
$scope.myrand=Math.random();
document.querySelector('#genbut').addEventListener('click',function(){
$scope.myrand=Math.random();
$scope.$digest();
});
});
<body ng-app="myhttpapp">
<div ng-controller="mycont">
<h1>{{myrand}}</h1>
<button type="button" id="genbut">generate</button>
</div>
</body>
Services:
var myapp=angular.module("myhttpapp",[]);
myapp.service("ran",function (){
var num=Math.floor(Math.random()*10);
this.name="hello";
this.generate=function(){
return num;
};
});
myapp.controller("mycont",function($scope,ran){
//$scope.myrand=Math.random();
document.querySelector('#genbut').addEventListener('click',function(){
$scope.myrand=ran.generate();
$scope.$digest();
});
});
Factories:
var myapp=angular.module("myhttpapp",[]);
myapp.factory("ran",function (){
var randobj={};
var num=Math.floor(Math.random()*10);
randobj.name="hello";
randobj.generate=function(){
return num;
};
return randobj;
});
myapp.controller("mycont",function($scope,ran){
//$scope.myrand=Math.random();
document.querySelector('#genbut').addEventListener('click',function(){
$scope.myrand=ran.generate();
$scope.$digest();
});
});
Providers:
var myapp=angular.module("myhttpapp",[]);
myapp.provider("date",function (){
return {
$get:function (){
return {
showdate:function (){
var date = new Date();
return date.getHours();
}
};
}
};
});
myapp.controller("mycont",function($scope,date){
//$scope.myrand=Math.random();
document.querySelector('#genbut').addEventListener('click',function(){
$scope.myrand=date.showdate();
$scope.$digest();
});
});
More Providers
var myapp=angular.module("myhttpapp",[]);
myapp.provider("date",function (){
var greet;
return {
setGreet:function(value){
greet=value;
},
$get:function (){
return {
showdate:function (){
var date = new Date();
return greet+" it is "+date.getHours()+":"+date.getMinutes();
},
devshowdate:function (){
var date = new Date();
return date.getHours();
}
};
}
};
});
myapp.config(function(dateProvider){
var time= dateProvider.$get().devshowdate();
if(time>0&&time<12){
dateProvider.setGreet("Good Morning!");
} else if(time>12&&time<17){
dateProvider.setGreet("Good AfterNoon!");
} else if(time>17&&time<19){
dateProvider.setGreet("Good Evening!");
} else{
dateProvider.setGreet("Good Night!");
}
});
myapp.controller("mycont",function($scope,date){
//$scope.myrand=Math.random();
document.querySelector('#genbut').addEventListener('click',function(){
$scope.myrand=date.showdate();
$scope.$digest();
});
});
NG_SHOW
<body ng-app="myhttpapp">
<div ng-controller="mycont">
<h1>{{myrand}}</h1>
<button type="button" id="genbut">generate</button>
<h3 ng-show="myrand==23">This is 23</h3>
<h3 ng-show="myrand!=23">This is not 23</h3>
</div>
</body>