angularjs基础用法
1、ng-app=” ” 定义angularJS的使用范围;
2、ng-init=”变量=值;变量=’值’” 初始化变量的值,有多个变量时,中间用分号隔开;
3、ng-model=”变量” 定义变量名;
4、ng-bind=”变量”
绑定变量名,获取该变量的数据。这里的变量就是第3条的变量名。但是一般都用双重花括号来获取变量的值,比如:{{变量}}。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.bootcss.com/angular.js/1.4.6/angular.min.js"></script>
</head>
<body ng-app="myApp">
<div class="runoob-directive"></div>
<script>
var app = angular.module("myApp", []);
app.directive("runoobDirective", function() {
return {
restrict : "C",
template : "<h1>自定义指令!</h1>"
};
});
</script>
<p><strong>注意:</strong> 你必须设置 <b>restrict</b> 的值为 "C" 才能通过类名来调用指令。</p>
</body>
</html>
结果:
自定义指令!
注意: 你必须设置 restrict 的值为 “C” 才能通过类名来调用指令。
</head>
<body ng-app="myApp">
<!-- directive: runoob-directive -->
<script>
var app = angular.module("myApp", []);
app.directive("runoobDirective", function() {
return {
restrict : "M",
replace : true,
template : "<h1>自定义指令!</h1>"
};
});
</script>
<p><strong>注意:</strong> 我们需要在该实例添加 <strong>replace</strong> 属性, 否则评论是不可见的。</p>
<p><strong>注意:</strong> 你必须设置 <b>restrict</b> 的值为 "M" 才能通过注释来调用指令。</p>
</body>
</html>
结果:
自定义指令!
注意: 我们需要在该实例添加 replace 属性, 否则评论是不可见的。
注意: 你必须设置 restrict 的值为 “M” 才能通过注释来调用指令。
restrict 值可以是以下几种:
E 作为元素名使用
A 作为属性使用
C 作为类名使用
M 作为注释使用
restrict 默认值为 EA, 即可以通过元素名和属性名来调用指令。
<!DOCTYPE html>
<html><head>
<meta charset="utf-8">
<script src="https://cdn.bootcss.com/angular.js/1.4.6/angular.min.js"></script>
</head>
<body ng-app="myApp" >
<div ng-controller = "DirectiveController">
<first></first>
<second></second>
</div>
<third></third>
<script>
angular.module('myApp', []).directive('first', [ function(){
return {
// scope: false, // 默认值,共享父级作用域
// controller: function($scope, $element, $attrs, $transclude) {},
restrict: 'AE', // E = Element, A = Attribute, C = Class, M = Comment
template: 'first name:{{name}}',
};
}]).directive('second', [ function(){
return {
scope: true, // 继承父级作用域并创建指令自己的作用域
controller: function($scope, $element, $attrs, $transclude) {$scope.name = "candy"},
restrict: 'AE', // E = Element, A = Attribute, C = Class, M = Comment
//当修改这里的name时,second会在自己的作用域中新建一个name变量,与父级作用域中的
// name相对独立,所以再修改父级中的name对second中的name就不会有影响了
template: 'second name:{{name}}',
};
}]).directive('third', [ function(){
return {
scope: {}, // 创建指令自己的独立作用域,与父级毫无关系
controller: function($scope, $element, $attrs, $transclude) {$scope.name = "nick"},
restrict: 'AE', // E = Element, A = Attribute, C = Class, M = Comment
template: 'third name:{{name}}',
};
}])
.controller('DirectiveController', ['$scope', function($scope){
$scope.name="mike";
}]);
</script>
</body>
</html>
结果:
first name:mike second name:candy third name:nick
// angular.module('MyApp',[])
// .directive('zl1',zl1)
// .controller('con1',['$scope',func1]);
//
// function zl1(){
// var directive={
// restrict:'AEC',
// template:'this is the it-first directive',
// };
// return directive;
// };
//
// function func1($scope){
// $scope.name="alice";
// }
//这是教程里类似的写法
angular.module('myApp',[]).directive('zl1',[ function(){
return {
restrict:'AE',
template:'thirective',
link:function($scope,elm,attr,controller){
console.log("这是link");
},
controller:function($scope,$element,$attrs){
console.log("这是con");
}
};
}]).controller('Con1',['$scope',function($scope){
$scope.name="aliceqqq";
}]);
CSS类:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.bootcss.com/angular.js/1.4.6/angular.min.js"></script>
<style>
input.ng-invalid {
background-color: lightblue;
}
</style>
</head>
<body>
<form ng-app="" name="myForm">
输入你的名字:
<input name="myName" ng-model="myText" required>
</form>
<p>编辑文本域,不同状态背景颜色会发送变化。</p>
<p>文本域添加了 required 属性,该值是必须的,如果为空则是不合法的。</p>
</body>
</html>
ng-model 指令根据表单域的状态添加/移除以下类:
ng-empty
ng-not-empty
ng-touched
ng-untouched
ng-valid
ng-invalid
ng-dirty
ng-pending
ng-pristine
ng-invalid:未通过验证的表单
ng-valid:布尔型属性,它指示表单是否通过验证。如果表单当前通过验证,他将为true
ng-dirty:布尔值属性,表示用户是否修改了表单。如果为ture,表示没有修改过;false表示修改过
ng-touched:布尔值属性,表示用户是否和控件进行过交互
ng-pristine:布尔值属性,表示用户是否修改了表单。如果为ture,表示没有修改过;false表示修改过