关于AngularJS的一些基础总结
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script> </head> <body> <div ng-app=""> <p>名字 : <input type="text" ng-model="name"></p> <h1>Hello {{name}}</h1> </div> </body> </html>
各个 angular.js 版本下载: https://github.com/angular/angular.js/releases
模块定义了一个应用程序。模块是应用程序中不同部分的容器。 <div ng-app="myApp" ng-controller="myCtrl"> </div>
<script type="text/javascript"> var app = angular.module('myApp’,[]); //创建模块 app.controller('myCtrl',function($scope){ }); </script>
使用模块来为你应用添加自己的指令: <div ng-app="myApp" runoob-directive></div>
<script> var app = angular.module("myApp",[]); app.directive("runoobDirective",function() { return { template : "我在指令构造器中创建!" }; }); </script>
//模块和控制器包含在 JS 文件中
//<script src="myApp.js"></script>
<script src="myCtrl.js"></script>
ng-repeat 指令会重复一个HTML元素,对于集合中(数组中)的每个项会 克隆一次 HTML 元素 <div ng-app="" ng-init="names=[ {name:'Jani',country:'Norway'},{name:'Hege',country:'Sweden'},{name:'Kai',country:'Denmark'}]"> <p>循环对象:</p> <ul> <li ng-repeat="x in names"> {{ x.name + ',' + x.country }} </li> </ul> </div>
$dirty//表单有填写记录
$valid//字段内容合法的
$invalid//字段内容是非法的
$pristine//表单没有填写记录
myForm.user.$dirty && myForm.user.$invalid
排序显示,可以使用 <tr ng-repeat="x in names | orderBy : 'Country'">
过滤器可以使用一个管道字符 currency//格式化数字为货币格式。
filter//从数组项中选择一个子集。
lowercase//格式化字符串为小写。
orderBy//根据某个表达式排列数组。
uppercase//格式化字符串为大写。
<p>姓名为 {{ lastName | uppercase }}</p>
var app = angular.module('myApp',[]);
app.controller('customersCtrl',function($scope,$location) {
$scope.myUrl = $location.absUrl();
});
<script> var app = angular.module('myApp',[]); app.controller('myCtrl',function($scope,$http) { $http.get("welcome.htm").then(function (response) { $scope.myWelcome = response.data; }); }); </script>
var app = angular.module('myApp',[]);
app.controller('myCtrl',$timeout) {
$scope.myHeader = "Hello World!";
$timeout(function () {
$scope.myHeader = "How are you today?";
},2000); //2000毫秒后执行
});
var app = angular.module('myApp',$interval) {
$scope.theTime = new Date().toLocaleTimeString();
$interval(function () {
$scope.theTime = new Date().toLocaleTimeString();
},1000);//每1000毫秒执行一次
});
创建自定义服务 <p>自定义服务,用于转换16进制数:</p>
<script> var app = angular.module('myApp',[]); app.service('hexafy',function() { this.myFunc = function (x) { return x.toString(16); } }); app.controller('myCtrl',hexafy) { $scope.hex = hexafy.myFunc(255); });
ng-options="x for (x,y) in cars"选择的值为在 key-value 对
ng-options有以下格式的语法
//for array data sources:
-----------------------
label for value in array
select as label for value in array label group by group for value in array select as label group by group for value in array track by trackexpr //for object data sources: ------------------------ label for (key,value) in object select as label for (key,value) in object label group by group for (key,value) in object select as label group by group for (key,value) in ob
$http.get("url").success(function ($){});
angular.lowercase()//转换字符串为小写
angular.uppercase()//转换字符串为大写
angular.isString()//判断给定的对象是否为字符串,如果是返回 true。
angular.isNumber()//判断给定的对象是否为数字,如果是返回 true。
angular.copy($scope.master) //复制对象
$scope.$watch('lName',function() {$scope.test();}); //监控模型变量
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular-animate.min.js"></script>
还需在应用中使用模型 var app = angular.module('myApp',['ngAnimate']);
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |