新聞中心
AngularJS指令可以為給你的訪問者提供更好的用戶體驗(yàn),比如通過展示用戶頭像來使頁面看起來更具個(gè)性化。在你的注冊表單中,可以在電子郵箱地址一欄的旁邊展示一個(gè)頭像,指示用戶輸入的是否是一個(gè)正確的郵件地址。如果在你的表單中有可選輸入項(xiàng),你可以默認(rèn)隱藏它們,當(dāng)用戶點(diǎn)擊時(shí)再展示出來,并且立刻自動將焦點(diǎn)對準(zhǔn)第一個(gè)輸入框。這些方法非常容易實(shí)現(xiàn),并且可以通過指令來獲得復(fù)用。

成都創(chuàng)新互聯(lián)主營莆田網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,app軟件定制開發(fā),莆田h5微信小程序定制開發(fā)搭建,莆田網(wǎng)站營銷推廣歡迎莆田等地區(qū)企業(yè)咨詢
你有許多方式來構(gòu)建AngularJS指令。關(guān)于如果創(chuàng)建用戶指令已經(jīng)有非常多的教程和指導(dǎo)(所以我不打算在此描述一些基本的東西):
-
AngularJS:開發(fā)者指南
-
Sidepoint:實(shí)用指南
-
...
我這里選取了三條對于提升用戶體驗(yàn)非常有幫助的指令,并且我很早就將其應(yīng)用在產(chǎn)品中。
1.頭像圖片
為了在你的應(yīng)用中展示頭像圖片,你需要使用用戶的電子郵件地址,將地址轉(zhuǎn)換為小寫并使用md5加密該字符串。所以聰明的做法是使用指令來做到這些,并且可以復(fù)用。
- /*
- * A simple Gravatar Directive
- * @example
- *
- */
- app.directive('gravatarImage', function () {
- return {
- restrict: 'AE',
- replace: true,
- required: 'email',
- template: '
',
- link: function (scope, element, attrs) {
- attrs.$observe('email', function (value) {
- if(!value) { return; }
- // MD5 (Message-Digest Algorithm) by WebToolkit
- var md5=function(s){function L(k,d){return(k<
>>(32-d));}function K(G,k){var I,d,F,H,x;F=(G&2147483648);H=(k&2147483648);I=(G&1073741824);d=(k&1073741824);x=(G&1073741823)+(k&1073741823);if(I&d){return(x^2147483648^F^H);}if(I|d){if(x&1073741824){return(x^3221225472^F^H);}else{return(x^1073741824^F^H);}}else{return(x^F^H);}}function r(d,F,k){return(d&F)|((~d)&k);}function q(d,F,k){return(d&k)|(F&(~k));}function p(d,F,k){return(d^F^k);}function n(d,F,k){return(F^(d|(~k)));}function u(G,F,aa,Z,k,H,I){G=K(G,K(K(r(F,aa,Z),k),I));return K(L(G,H),F);}function f(G,F,aa,Z,k,H,I){G=K(G,K(K(q(F,aa,Z),k),I));return K(L(G,H),F);}function D(G,F,aa,Z,k,H,I){G=K(G,K(K(p(F,aa,Z),k),I));return K(L(G,H),F);}function t(G,F,aa,Z,k,H,I){G=K(G,K(K(n(F,aa,Z),k),I));return K(L(G,H),F);}function e(G){var Z;var F=G.length;var x=F+8;var k=(x-(x%64))/64;var I=(k+1)*16;var aa=Array(I-1);var d=0;var H=0;while(H >>29;return aa;}function B(x){var k="",F="",G,d;for(d=0;d<=3;d++){G=(x>>>(d*8))&255;F="0"+G.toString(16);k=k+F.substr(F.length-2,2);}return k;}function J(k){k=k.replace(/rn/g,"n");var d="";for(var F=0;F 127)&&(x<2048)){d+=String.fromCharCode((x>>6)|192);d+=String.fromCharCode((x&63)|128);}else{d+=String.fromCharCode((x>>12)|224);d+=String.fromCharCode(((x>>6)&63)|128);d+=String.fromCharCode((x&63)|128);}}}return d;}var C=Array();var P,h,E,v,g,Y,X,W,V;var S=7,Q=12,N=17,M=22;var A=5,z=9,y=14,w=20;var o=4,m=11,l=16,j=23;var U=6,T=10,R=15,O=21;s=J(s);C=e(s);Y=1732584193;X=4023233417;W=2562383102;V=271733878;for(P=0;P - scope.hash = md5(value.toLowerCase());
- scope.size = attrs.size;
- if(angular.isUndefined(scope.size)) {
- scope.size = 60; // default to 60 pixels
- }
- });
- }
- };
- });
- 2. Focus-Me
- It is really just a small directive, but it’s awesome. In the example below the user clicks on a link, where he makes an input visible, which gets automatically focused. So he doesn’t need to click in the input field when it shows up.
- /**
- * Sets focus to this element if the value of focus-me is true.
- * @example
- * add name
- *
- */
- app.directive('focusMe', ['$timeout', function($timeout) {
- return {
- scope: { trigger: '@focusMe' },
- link: function(scope, element) {
- scope.$watch('trigger', function(value) {
- if(value === "true") {
- $timeout(function() {
- element[0].focus();
- });
- }
- });
- }
- };
- }]);
2. 關(guān)注我
這其實(shí)是一個(gè)非常簡短的指令,但是非常棒。在下面的例子中,用戶點(diǎn)擊了一個(gè)鏈接,顯示的輸入框需要能夠自動獲得焦點(diǎn)。這樣,用戶在頁面顯示時(shí)不必再手動點(diǎn)擊文本域。
- /**
- * Sets focus to this element if the value of focus-me is true.
- * @example
- * add name
- *
- */
- app.directive('focusMe', ['$timeout', function($timeout) {
- return {
- scope: { trigger: '@focusMe' },
- link: function(scope, element) {
- scope.$watch('trigger', function(value) {
- if(value === "true") {
- $timeout(function() {
- element[0].focus();
- });
- }
- });
- }
- };
- }]);
3.Contenteditable元素模型綁定
我們使用contenteditable而不是textarea元素的最主要原因在于使用前者可以在布局和UI中沒有限制。我們在編輯器中使用這條指令可以實(shí)現(xiàn)將contenteditable元素的html和ng-model進(jìn)行一個(gè)雙向綁定。目前,在contenteditable元素中并沒有支持ng-model。
- /**
- * Two-way data binding for contenteditable elements with ng-model.
- * @example
- *
- */
- app.directive('contenteditable', function() {
- return {
- require: '?ngModel',
- link: function(scope, element, attrs, ctrl) {
- // Do nothing if this is not bound to a model
- if (!ctrl) { return; }
- // Checks for updates (input or pressing ENTER)
- // view -> model
- element.bind('input enterKey', function() {
- var rerender = false;
- var html = element.html();
- if (attrs.noLineBreaks) {
- html = html.replace(/
/g, '').replace(/
/g, '').replace(/<\/div>/g, '');- rerender = true;
- }
- scope.$apply(function() {
- ctrl.$setViewValue(html);
- if(rerender) {
- ctrl.$render();
- }
- });
- });
- element.keyup(function(e){
- if(e.keyCode === 13){
- element.trigger('enterKey');
- }
- });
- // model -> view
- ctrl.$render = function() {
- element.html(ctrl.$viewValue);
- };
- // load init value from DOM
- ctrl.$render();
- }
- };
- });
結(jié)論:AngularJS指令可用于改善用戶體驗(yàn)
我希望經(jīng)過文中的介紹,你會感悟到AngularJS指令的有用之處。
對我而言,指令是AngularJS中最激動人心的特性。創(chuàng)建可重用的組件,并可以將其添加到純粹的HTML應(yīng)用程序庫,這是多么難以置信并且強(qiáng)大的功能。由于指令實(shí)用,并且大部分指令書寫難度不高,許多開發(fā)者早已對于目前受歡迎的庫開發(fā)了許多指令。舉例來說,AngularJS團(tuán)隊(duì)已經(jīng)為Bootstrap創(chuàng)建了一系列的指令(難道還有人不用它嗎?),被稱作UI Bootstrap。
英文:Improve user experience with these 3 AngularJS directives
譯文:http://www.oschina.net/translate/better-ux-with-angularjs-directives
網(wǎng)站標(biāo)題:改善用戶體驗(yàn)的3個(gè)AngularJS指令
文章地址:http://www.dlmjj.cn/article/dpgched.html


咨詢
建站咨詢
