AngularJS 学习笔记(3)-指令(Directive)
指令(Directive)
指令API文档>>> ng-app
<!DOCTYPE html>
<html lang="zh-CN" ng-app="MyApp"> <!--常放于此-->
<head>
<meta charset="UTF-8">
<title>ng-app 指令</title>
</head>
<body>
<script> var myp = angular.module("MyApp",[]); myp.controller('HelloController',['$scope',function($scope) { $scope.p = {name: 'MyName'}; } ]); </script>
</body>
</html>
ng-repeat
<body ng-app="myApp"> <ul ng-controller="ListController"> <!-- ng-repeat 会遍历数组中每一个元素,分别创建li --> <li ng-repeat="item in items" data-id="{{item.id}}"> <p>{{item.name}}</p> </li> </ul> <script src="../angular/angular.js"></script> <script> angular.module('myApp',[]) .controller('ListController',function($scope) { $scope.items= []; for (var i = 1; i < 10; i++) { $scope.items[$scope.items.length] = { id: i,name: 'id:' + i,}; } }]); </script> </body>
ng-bindng-bind指令在绑定的值包含HTML时会转义,为了安全(跨站脚本攻击) <body ng-app ng-init="username='<h1>shit</h1>'"> <!-- <strong>{{username}}</strong> --> <strong ng-bind="username"></strong> <script src="../angular/angular.js"></script> </body>
ng-bind-html
<body ng-app="myApp" ng-init="username='<h1>shit</h1>'"> <!-- <strong>{{username}}</strong> --> <!-- ng-bind指令在绑定的值包含HTML时会转义,为了安全(跨站脚本攻击) --> <strong ng-bind-html="username"></strong> <script src="../angular/angular.js"></script> <script src="../angular-sanitize/angular-sanitize.js"></script> <script> // 使用自定义的模块才可以依赖别的包里面定义模块,angular定义的默认模块没有依赖任何 angular.module('myApp',['ngSanitize']); </script>
ng-class
<ul class="messages"> <li ng-repeat="item in messages track by $index" ng-class="{red:item.read}"> {{item.content}} </li> </ul>
ng-show/ng-hide
ng-if
保留 HTML:
<input type="checkbox" ng-model="myVar" ng-init="myVar = true">
<div ng-if="myVar">
<h1>Welcome</h1>
<p>Welcome to my home.</p>
<hr>
</div>
尝试一下>>> ng-href/ng-src
-
<img ng-src="{{imgUrl}}" alt=""> <a ng-href="{{imgUrl}}">跳转到图片</a>
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |