model
变化,view
也变化 {:&.moveIn}解析
到DOM中模块化
与依赖注入
创建自定义的标签
,也可以用来装饰元素
或者操作DOM
属性。npm install angular
bower install angular
<div ng-bind-template="{{name}} {{age}}"></div>
通常情况下,不使用 ng-init。使用一个控制器或模块来代替它。
<div ng-init="phones=[{name:[{a:1}]},{name:[{a:1}]}]">
<div ng-repeat="phone in phones" ng-init="c=$index">
<div ng-repeat="a in phone.name" ng-init="b=$index">
{{c}}{{b}}
</div>
</div>
</div>
<input type="text" ng-model="name">
<div ng-switch="name">
<p ng-switch-when="by">123</p>
<p ng-switch-when="hello">456</p>
<p ng-switch-when="girl">789</p>
<p ng-switch-default>10000</p>
</div>
<div class="alert" ng-class="{'alert-success':success=='alert-success','alert-info':success=='alert-info'}">...</div>
<ul class="nav nav-tabs">
<li role="presentation" ng-click="click='Home'" ng-class="{active:click=='Home'}"><a href="">Home</a></li>
<li role="presentation" ng-click="click='Profile'" ng-class="{active:click=='Profile'}"><a href="">Profile</a></li>
<li role="presentation" ng-click="click='Messages'" ng-class="{active:click=='Messages'}"><a href="">Messages</a></li>
</ul>
{{100 | currency:'£' }}
{{'abc' | uppercase }}
{{'ABC' | lowercase }}
{{123456 | limitTo:5}}
{{1234.2345|number:2}}
<pre>
{{{aa:123,bb:456} | json}}
</pre>
{{1654325689063 | date:'yyyy-MM-dd hh:mm:ss'}}
<div ng-repeat="p in phones | orderBy:'-age':'reverse'">
{{p.name}}
</div>
<div ng-repeat="p in phones | filter:{age:query}">
{{p.name}}
</div>
var abc = angular.uppercase("aaaa");
console.log(abc);
var abc = angular.lowercase("aaaa");
console.log(abc);
var a = angular.equals(NaN,NaN);
console.log(a);
var obj = {a:123},obj1 = {b:456};
angular.extend(obj,obj1);
console.log(obj);
var obj = '{"aa":123,"bb":456}';
var a =angular.fromJson(obj);
a = angular.toJson(a);
console.log(a);
var obj = {a:123},obj1 = {b:456};
angular.copy(obj,obj1);
console.log(obj1);
var arr = [{name:1},{name:2},{name:3}];
var result = [];
angular.forEach(arr,function (item) {
this.push(item.name);
},result);
var obj = {name:2};
function arr(who){console.log(this.name+who);}
var newArr = angular.bind(obj,arr,1);
newArr();
模型
层 {:&.moveIn}视图
层,负责展示控制
逻辑MVC只是手段,目标是模块化和复用
app.controller('appCtrl',['$scope',function ($s) {
$s.name = {a:123}
}]);
md.run(["$rootScope",function($s){
}]);
$watch 方法监视 Model 的变化 {:&.moveIn}
$s.$watch('name', function (newValue,oldValue) {
console.log(newValue,oldValue);
});
$apply你需要命令 AngularJS 刷新自已(模型、视图等)
$scope.$apply(function(){})
<a ng-href="{{ myHref }}">baidu</a>
var app = angular.module('appModule',[]);
app.controller('appCtrl',function ($scope,$timeout) {
$timeout(function () {
$scope.myHref = 'http://www.baidu.com'
},20000)
})
##ng-src
<img ng-src="{{imgSrc}}"/>
var app = angular.module('appModule',[]);
app.controller('appCtrl',function ($scope,$timeout) {
$timeout(function() {
$scope.imgSrc = 'https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png';
}, 1000);
})
ng-model-options="{ updateOn: 'blur' }"
app.controller('totalCtrl', function ($scope,$interval) {
$scope.name = "点击获取";$scope.flag = false;
$scope.change = function () {
$scope.flag = true;$scope.name = '10秒后';
var i = 10;
var timer = $interval(function () {
i--;$scope.name = i+'秒后';
if(i<=0){
$interval.cancel( timer );
$scope.flag = false;
}
},100);
}
});
<input type="text" ng-readonly="stop" value="3秒后禁用"/>
.run(function($rootScope,$timeout){
$rootScope.stop=false;
$timeout(function(){
$rootScope.stop = true;
},3000)
})
var app = angular.module('formApp',['formApp1']);
app.controller('totalCtrl', function ($scope,$interval) {
$scope.name = 1;
});
var app1 = angular.module('formApp1',[]);
app1.controller('totalCtrl', function ($scope,$interval) {
$scope.name = 20;
});
document.body.onclick = function () {
angular.bootstrap(div1,['formApp1']);
angular.bootstrap(div2,['formApp2']);
}
$http {:&.moveIn}
$http(
{
method:'GET',
url:'person.json'
}).success(function(data,status,headers,config){
$scope.books = data;
}).error(function(data,status,headers,config){
})
$http.get('/ajax1').success(function (data,status,headers,config) {
$scope.data = data;
});
$http.post('/ajax',{data:2}).success(function (data,status,headers,config) {
$scope.data = data;
})
$http.jsonp('/ajax3').success(function (data,status,headers,config) {
$scope.data = data;
})
angular.callbacks._0([{name:1}]);
app.controller('remoteCtrl', function ($scope,$http,numberFilter) {
$scope.total = 123.333;
$scope.newFilter = numberFilter($scope.total,2)
});
var app = angular.module('formApp1',[]);
app.controller('totalCtrl', function ($scope,$http,dateFilter) {
$scope.name = dateFilter(new Date(),'yyyy-MM')
});
app.filter('some', function (uppercaseFilter) {
return function (content,length) {
return uppercaseFilter(content.slice(0,length))+content.slice(length);
}
});
app.config(function (myProvider) {
myProvider.setCurrency = '%';
});
app.provider('my', function () {
this.setCurrency = '$';
this.$get = function () {
var that =this;
return {
minus: function (a,b) {
return that.setCurrency +(a-b)
}
}
};
});
app.factory('my', function () {
var obj = {name:1,age:2}
return {
obj:obj
}
});
app.service('my', function () {
this.set = function () {
return 1;
};
this.get = function () {
return 2;
}
});
app.config(function (my) {
// my.bb=456;
})
app.constant('my',{
set: function () {
return 1
},
get: function () {
return 2
}
});
app.constant('my',{
set: function () {
return 1
},
get: function () {
return 2
}
});
app.factory("Phone", function ($resource) {
return $resource('user/:uid',{
uid:'@uid'
},{
update:{//增加一个update 发起put请求
method:'PUT'
}
});
})
note.query({id:1});//GET
note.save({id:1,name:2});//POST
note.delete({id:1});//DELET
note.update({id:1});//PUT
app.config(function ($routeProvider) {
$routeProvider.when('/',{
templateUrl:'./tmpl/home.html',
controller:'HomeCtrl'
}).when('/user',{
templateUrl:'./tmpl/next.html',
controller:'HomeCtrl'
}).otherwise({
redirectTo:'/'
})
});