Angular下的directive(part1)
<body>
or <html>标签。
angular.bootstrap来代替。
angular.module中有跟多的信息
下面的这个例子中,
如果ngApp指令没有放在html文档不会被编译,AppController将不能够被实例化为{{ a+b }} 等于 3
所以要这么写
angular.module('ngAppDemo', []).controller('ngAppDemoController', function($scope) {
$scope.a = 1;
$scope.b = 2;
});
<ANY
ng-app="">
...
</ANY>
Param | Type | Details |
---|---|---|
ngApp | angular.Module |
可配置应用程序模块被载入 |
修改A标签的默认行为,阻止默认动作当Href属性为空的时候。
<a href="" ng-click="list.addItem()">Add Item</a>
<a>
...
</a>
///////////////////////////////////////////////////////
<body ng-app="ngAppDemo">
<div ng-controller="ngAppDemoController">
<a href="" ng-click="list.addItem()">Add Item</a>
</div>
<script type="text/javascript">
angular.module('ngAppDemo', []).controller('ngAppDemoController', function($scope) {
$scope.list = {
addItem: function() {
alert("test");
}
}
});
</script>
</body>
Angular是用{{}}来标记的,在用户点击的时候angrual还咩有去替换href属性中{{}}值。这样
angular替换链接就被失败了,通常显示为 404 错误
ngHref指令将解决这个问题 (指令由angrular自带的,也有自己定义的 日后会知道指令的重要性)
错误的写法是:
<a href="http://www.gravatar.com/avatar/{{hash}}"/>
正确的方法去写它:
<a ng-href="http://www.gravatar.com/avatar/{{hash}}"/>
<A
ng-href="">
...
</A>
Param | Type | Details |
---|---|---|
ngHref | template |
任何字符串可以包含{{}}里。
|
不建议的写法:
<img src="http://www.gravatar.com/avatar/{{hash}}"/>
<img ng-src="http://www.gravatar.com/avatar/{{hash}}"/>
ng-src="">
...
</IMG>
ArgumentsParam | Type | Details |
---|---|---|
ngSrc | template |
任何字符串可以包含在 |
不建议的写法:
<img srcset="http://www.gravatar.com/avatar/{{hash}} 2x"/>
建议的方案:
<img ng-srcset="http://www.gravatar.com/avatar/{{hash}} 2x"/>
<IMG
ng-srcset="">
...
</IMG>
Param | Type | Details |
---|---|---|
ngSrcset | template |
任何字符串可以包含在 |
<div ng-init="scope = { isDisabled: false }">
<button disabled="{{scope.isDisabled}}">Disabled</button>
</div>
<INPUT
ng-disabled="">
...
</INPUT>
///////////////////////////////////////////////////////
Click me to toggle: <input type="text" ng-model="youname"><br/>
{{youname}}
<button ng-disabled="youname">Button</button>
ng-disabled="youname"写成字符串的时候就可以实现,如果用{{}}就不可用。
Param | Type | Details |
---|---|---|
ngDisabled | expression |
如果表达式为真,那么这个特别的属性“disabled”将被禁用 |
Directive Info
<INPUT
ng-checked="">
...
</INPUT>
//////////////////////////////////////
Check me to check both: <input type="checkbox" ng-model="master"><br/>
<input >
Param | Type | Details |
---|---|---|
ngChecked | xpression |
如果表达式为真,那么这个特别的属性“checked” 将被设置 |
eadonly
的属性,这种互补的指令不会被浏览器撤销,因此Directive Info
<INPUT
ng-readonly="">
...
</INPUT>
//////////////////////////////////////////////////
Check me to make text readonly: <input type="checkbox" ng-model="checked"><br/>
<input type="text" ng-readonly="checked" value="I'm Angular"/>
ArgumentsParam | Type | Details |
---|---|---|
ngReadonly | expression |
如果表达式为真,那么这个特别的属性“checked” 将被设置 |
Param | Type | Details |
---|---|---|
ngSelected | expression |
如果表达式为真,那么这个特别的属性“selected” 将被设置 |
<ng-form
[name=""]>
...
</ng-form>
as attribute: //作为属性
<ANY
[ng-form=""]>
...
</ANY>
as CSS class://作为累,样式
<ANY class="[ng-form: ;]"> ... </ANY>
Param | Type | Details |
---|---|---|
ngForm | name
(optional)
|
string |
表单的名称,如果指定,控制器将发表形式到相关名字范围,
|
<textarea
ng-model=""
[name=""]
[required=""]
[ng-required=""]
[ng-minlength=""]
[ng-maxlength=""]
[ng-pattern=""]
[ng-change=""]
[ng-trim=""]>
...
</textarea>
Param | Type | Details |
---|---|---|
ngModel | string |
可指定的angular数据绑定表达式 |
name
(optional)
|
string |
属性名的形式控制发表。
|
required
(optional)
|
string |
如果你的值没有键入,那么设置 required 验证错误的关键 |
ngRequired
(optional)
|
string |
添加所需的属性,要求验证约束元素ngRequired表达式的求值结果为true。使用ngRequired而不是必需的当你想要数据绑定到所需的属性。
|
ngMinlength
(optional)
|
number |
如果键入值比最短输入值还少的话,设置的 minlength 就会生效提示错误 |
ngMaxlength
(optional)
|
number |
如果键入值比最短输入值还多的话,设置的 maxlength 就会生效提示错误 |
ngPattern
(optional)
|
string |
如果不匹配正则,pattern 错误就会生效,预期值/正则表达式/内联模式或者regexp模式定义为范围表达式。 |
ngChange
(optional)
|
string |
Angular表达式被执行当输入发生变化时由于输入用户交互元素。 |
ngTrim
(optional)
|
boolean |
如果设置为false Angular不会减少Input的输入【减少两遍的空格】 (default: true)--->默认为true |
input控制着一下的一些HTML5表单类型以及对一个老版本的HTML5的验证行为。
<ng-form
[name=""]>
...
</ng-form>
as attribute: //作为属性
<ANY
[ng-form=""]>
...
</ANY>
as CSS class://作为累,样式
<ANY class="[ng-form: ;]"> ... </ANY>
Param | Type | Details |
---|---|---|
ngForm | name
(optional)
|
string |
表单的名称,如果指定,控制器将发表形式到相关名字范围,
|
<textarea
ng-model=""
[name=""]
[required=""]
[ng-required=""]
[ng-minlength=""]
[ng-maxlength=""]
[ng-pattern=""]
[ng-change=""]
[ng-trim=""]>
...
</textarea>
Param | Type | Details |
---|---|---|
ngModel | string |
可指定的angular数据绑定表达式 |
name
(optional)
|
string |
属性名的形式控制发表。
|
required
(optional)
|
string |
如果你的值没有键入,那么设置 required 验证错误的关键 |
ngRequired
(optional)
|
string |
添加所需的属性,要求验证约束元素ngRequired表达式的求值结果为true。使用ngRequired而不是必需的当你想要数据绑定到所需的属性。
|
ngMinlength
(optional)
|
number |
如果键入值比最短输入值还少的话,设置的 minlength 就会生效提示错误 |
ngMaxlength
(optional)
|
number |
如果键入值比最短输入值还多的话,设置的 maxlength 就会生效提示错误 |
ngPattern
(optional)
|
string |
如果不匹配正则,pattern 错误就会生效,预期值/正则表达式/内联模式或者regexp模式定义为范围表达式。 |
ngChange
(optional)
|
string |
Angular表达式被执行当输入发生变化时由于输入用户交互元素。 |
ngTrim
(optional)
|
boolean |
如果设置为false Angular不会减少Input的输入【减少两遍的空格】 (default: true)--->默认为true |
input控制着一下的一些HTML5表单类型以及对一个老版本的HTML5的验证行为。
ng-valid
, ng-invalid
, ng-dirty
, ng-pristine
, ng-touched
, ng-untouched
)包括动画。
<script>
angular.module('inputExample', [])
.controller('ExampleController', ['$scope', function($scope) {
$scope.val = '1';
}]);
</script>
<style>
.my-input {
-webkit-transition:all linear 0.5s;
transition:all linear 0.5s;
background: transparent;
}
.my-input.ng-invalid {
color:white;
background: red;
}
</style>
Update input to see transitions when valid/invalid.
Integer is a valid value.
<form name="testForm" ng-controller="ExampleController">
<input ng-model="val" ng-pattern="/^\d+$/" name="anim" class="my-input" />
</form>
<script>
angular.module('changeExample', [])
.controller('ExampleController', ['$scope', function($scope) {
$scope.counter = 0;
$scope.change = function() {
$scope.counter++;
};
}]);
</script>
<div ng-controller="ExampleController">
<input type="checkbox" ng-model="confirmed" ng-change="change()" />
<input type="checkbox" ng-model="confirmed" />
<label for="ng-change-example2">Confirmed</label><br />
<tt>debug = {{confirmed}}</tt><br/>
<tt>counter = {{counter}}</tt><br/>
</div>
ng-list=" | ".
input[select]
or input[radio],
所以当元素被选中selected的时候ngModel元素设置为绑定值也就是value的值。
<script>
angular.module('valueExample', [])
.controller('ExampleController', ['$scope', function($scope) {
$scope.names = ['pizza', 'unicorns', 'robots'];
$scope.my = { favorite: 'unicorns' };
}]);
</script>
<form ng-controller="ExampleController">
<h2>Which is your favorite?</h2>
<label ng-repeat="name in names" for="{{name}}">
{{name}}
<input type="radio"
ng-model="my.favorite"
ng-value="name"
name="favorite">
</label>
<div>You chose {{my.favorite}}</div>
</form>
Param | Type | Details |
---|---|---|
ngModelOptions | object |
选择适用于当前的模型。有效值是:
updateOn :字符串指定应该输入绑定到哪个事件。你可以使用一个空间分隔的列表设置几个事件。有一个特殊事件
现在仅支持值是UTC,否则将使用浏览器的默认时区。
|
<ANY
ng-bind="">
...
</ANY>
<ANY class="ng-bind: ;"> ... </ANY>
Param | Type | Details |
---|---|---|
ngBind | expression |
表达式来评估 |
<script>
angular.module('bindExample', [])
.controller('ExampleController', ['$scope', function($scope) {
$scope.name = 'Whirled';
}]);
</script>
<div ng-controller="ExampleController">
Enter name: <input type="text" ng-model="name"><br>
Hello <span ng-bind="name"></span>!
</div>
<ANY
ng-bind-template="">
...
</ANY>
Param | Type | Details |
---|---|---|
ngBindTemplate | string |
模板从 |
<script>
angular.module('bindExample', [])
.controller('ExampleController', ['$scope', function($scope) {
$scope.salutation = 'Hello';
$scope.name = 'World';
}]);
</script>
<div ng-controller="ExampleController">
Salutation: <input type="text" ng-model="salutation"><br>
Name: <input type="text" ng-model="name"><br>
<pre ng-bind-template="{{salutation}} {{name}}!"></pre>
</div>
<ANY
ng-bind-html="">
...
</ANY>
Param | Type | Details |
---|---|---|
ngBindHtml | Expression |
解析表达式. |
<ANY
ng-class="">
...
</ANY>
as CSS class:
<ANY class="ng-class: ;"> ... </ANY>
ngClassOdd
and ngClassEven 完全按照ngclass来工作,
除了他们在一起工作,<ANY class="ng-class-odd: ;"> ... </ANY>
Param | Type | Details |
---|---|---|
ngClassOdd | expression |
表达式eval,结果可以是一个字符串,用空格分割类或一个数组。
|