AngularJS優(yōu)雅的自定義指令
學(xué)習(xí)要點(diǎn)
•為什么使用指令
•創(chuàng)建自定義指令
•使用jqLite工作
一、為什么使用自定義指令
NG內(nèi)置了許多自定義指令,但是它們有時(shí)并不能滿(mǎn)足你的要求,這是需要我們創(chuàng)建自定義屬性。
二、自定義指令
接下來(lái),我們來(lái)做一個(gè)小案例,當(dāng)鼠標(biāo)單擊加價(jià)后,列表項(xiàng)自動(dòng)遞增,當(dāng)然列表也是通過(guò)指令自動(dòng)添加的,它本就是一個(gè)空的div
<!DOCTYPE> <!-- use module --> <html ng-app="exampleApp"> <head> <title>Angluar test</title> <meta charset="utf-8"/> <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css"> <link rel="stylesheet" type="text/css" href="css/bootstrap-theme.min.css"> </head> <body> <dlv class="panel panel-default" ng-controller="defaultCtrl"> <div class="panel-heading"> <h3>Products</h3> </div> <div class="panel-body"> <!-- 點(diǎn)擊加價(jià),價(jià)格遞增 --> <button type="button" class="btn btn-primary" ng-click="incrementPrices()">加價(jià)</button> </div> <div class="panel-body"> <!-- 將products數(shù)據(jù)以一種無(wú)序列表的形式展示 --> <!-- list-property="price | currency" 列表項(xiàng)單位本地化 --> <div unorderlist="products" list-property="price | currency"></div> </div> </dlv> <script type="text/javascript" src="js/angular.min.js"></script> <script type="text/javascript"> angular.module("exampleApp", []) .directive("unorderlist", function () { // scope 作用域 // element 應(yīng)用該指令的元素 // attrs 使用該指令的元素的屬性 return function (scope, element, attrs) { // attrs['unorderlist'] 獲取unorderlist屬性值,這里為products // 獲取數(shù)據(jù)模型值,這里為scope.products var data = scope[attrs['unorderlist']]; // 創(chuàng)建一個(gè)<ul>標(biāo)簽元素 var ul = angular.element("<ul>"); // 在應(yīng)用該指令的元素中添加<ul> element.append(ul); // 獲取listProperty屬性值,這里為price | currency var expression = attrs['listProperty']; // 判斷是否為數(shù)組 if (angular.isArray(data)) { // 遍歷數(shù)據(jù)模型scope.products for (var i = 0; i < data.length; i++) { // 添加閉包,將i傳遞進(jìn)去 (function (index) { // 創(chuàng)建一個(gè)<li>標(biāo)簽元素 var li = angular.element("<li>"); // 將<li>標(biāo)簽添加到<ul>中 ul.append(li); // 監(jiān)聽(tīng)器函數(shù),用$eval計(jì)算表達(dá)式和data[index]的值 var watcherFn = function (watchScope) { return watchScope.$eval(expression, data[index]); } // 添加$watch監(jiān)聽(tīng)器監(jiān)聽(tīng)作用域的變化 scope.$watch(watcherFn, function (newValue, oldValue) { // 更新li的值 li.text(newValue); }) })(i); } } } }) .controller("defaultCtrl", function ($scope) { // 數(shù)據(jù)模型 $scope.products = [ { name: "Apples", category: "Fruit", price: 1.20, expiry: 10 }, { name: "Bananas", category: "Fruit", price: 2.42, expiry: 7 }, { name: "Pears", category: "Fruit", price: 2.02, expiry: 6 } ]; // 遞增價(jià)格 $scope.incrementPrices = function () { for (var i = 0; i < $scope.products.length; i++) { $scope.products[i].price++; } } }) </script> </body> </html>
解析:
第一步:創(chuàng)建控制器,添加數(shù)據(jù)模型products和遞增價(jià)格incrementPrices()方法
第二步:自定義unorderlist標(biāo)簽,該標(biāo)簽的作用是:通過(guò)作用域數(shù)據(jù)模型的值,將其值以無(wú)序列表的方式展示出來(lái)
第三部:并且在單擊加價(jià)按鈕時(shí),無(wú)序列表的值會(huì)依次遞增
三、使用jqLite工作
NG內(nèi)置了jqLite,它是JQuery的縮小版
<!DOCTYPE> <!-- use module --> <html ng-app="exampleApp"> <head> <title>Angluar test</title> <meta charset="utf-8"/> <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css"> <link rel="stylesheet" type="text/css" href="css/bootstrap-theme.min.css"> </head> <body> <dlv class="panel panel-default"> <!-- 使用自定義指令 --> <ol dome-directive> <li>Apples</li> <ul> <li>Bananas</li> <li>Cherries</li> <li>Oranges</li> </ul> <li>Pears</li> <li>Oranges</li> </ol> </dlv> <script type="text/javascript" src="js/angular.min.js"></script> <script type="text/javascript"> angular.module("exampleApp", []) .directive("domeDirective", function () { return function (scope, element, attrs) { // 找出element元素下所有的li,這里的element是調(diào)用者<ol> var items = element.find("li"); // 要所有的li的font-weight為bold items.css("font-weight", "bold"); // 遍歷li, 值為Oranges的字體顏色為red,其余為green for (var i = 0; i < items.length; i++) { if (items.eq(i).text() == "Oranges"){ items.eq(i).css("color", "red"); } else { items.eq(i).css("color", "green"); } } // 打印出li的長(zhǎng)度和字體顏色 console.log("Items length : " + items.length); console.log("Color: " + items.css("color")); } }) </script> </body> </html>
解析:
第一步:自定義控制器,定義數(shù)據(jù)模型names
第二步:自定義指令,功能是將使用指令的元素下的所有l(wèi)i找出,并且更加值的不同賦給字體不同的顏色
第三步:在視圖中調(diào)用并且使用指令
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 深入講解AngularJS中的自定義指令的使用
- AngularJS創(chuàng)建自定義指令的方法詳解
- AngularJS使用自定義指令替代ng-repeat的方法
- AngularJS 自定義指令詳解及實(shí)例代碼
- AngularJS自定義指令實(shí)現(xiàn)面包屑功能完整實(shí)例
- AngularJS實(shí)現(xiàn)自定義指令與控制器數(shù)據(jù)交互的方法示例
- AngularJS 自定義指令詳解及示例代碼
- AngularJS自定義指令之復(fù)制指令實(shí)現(xiàn)方法
- AngularJS自定義指令詳解(有分頁(yè)插件代碼)
- 詳解angularJS自定義指令間的相互交互
- AngularJS實(shí)現(xiàn)自定義指令及指令配置項(xiàng)的方法
相關(guān)文章
詳解Angular Reactive Form 表單驗(yàn)證
本文我們將介紹 Reactive Form 表單驗(yàn)證的相關(guān)知識(shí),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07angularJS實(shí)現(xiàn)表格部分列展開(kāi)縮起示例代碼
這篇文章主要介紹了angularJS實(shí)現(xiàn)表格部分列展開(kāi)縮起示例代碼,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09AngularJS中的路由使用及實(shí)現(xiàn)代碼
本篇文章主要介紹了AngularJS中的路由使用及實(shí)現(xiàn)代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10Angular單元測(cè)試之事件觸發(fā)的實(shí)現(xiàn)
這篇文章主要介紹了Angular單元測(cè)試之事件觸發(fā)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01AngularJS入門(mén)教程之路由機(jī)制ngRoute實(shí)例分析
這篇文章主要介紹了AngularJS入門(mén)教程之路由機(jī)制ngRoute,結(jié)合實(shí)例形式分析了AngularJS路由機(jī)制的原理、ngRoute的組成、配置、參數(shù)與相關(guān)使用技巧,需要的朋友可以參考下2016-12-12基于datepicker定義自己的angular時(shí)間組件的示例
這篇文章主要介紹了基于datepicker定義自己的angular時(shí)間組件,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03angularJS實(shí)現(xiàn)不同視圖同步刷新詳解
今天小編就為大家分享一篇angularJS實(shí)現(xiàn)不同視圖同步刷新詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10Angular在模板驅(qū)動(dòng)表單中自定義校驗(yàn)器的方法
本章介紹的是如何對(duì)模板驅(qū)動(dòng)表單創(chuàng)建自定義校驗(yàn)器,它相比較響應(yīng)式表單自定義校驗(yàn)器略為復(fù)雜一些。接下來(lái)通過(guò)本文給大家分享Angular在模板驅(qū)動(dòng)表單中自定義校驗(yàn)器的方法,感興趣的朋友一起看看吧2017-08-08Angular+Ionic使用queryParams實(shí)現(xiàn)跳轉(zhuǎn)頁(yè)傳值的方法
這篇文章主要介紹了Angular+Ionic使用queryParams實(shí)現(xiàn)跳轉(zhuǎn)頁(yè)傳值的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09