本文将介绍不应该有AngularJSngWith指令吗?的详细情况,特别是关于angularjs指令的作用的相关信息。我们将通过案例分析、数据研究等多种方式,帮助您更全面地了解这个主题,同时也将涉及一
本文将介绍不应该有AngularJS ngWith指令吗?的详细情况,特别是关于angularjs指令的作用的相关信息。我们将通过案例分析、数据研究等多种方式,帮助您更全面地了解这个主题,同时也将涉及一些关于3个可以改善用户体验的AngularJS指令介绍_AngularJS、angularjs – Angular可以在数值范围内使用ngSwitch吗?、angularjs – WinJS Angular Pivot指令无法呈现、angularjs – 可以在AngulrJS 1.3.0项目中使用angular / di.js吗?的知识。
本文目录一览:- 不应该有AngularJS ngWith指令吗?(angularjs指令的作用)
- 3个可以改善用户体验的AngularJS指令介绍_AngularJS
- angularjs – Angular可以在数值范围内使用ngSwitch吗?
- angularjs – WinJS Angular Pivot指令无法呈现
- angularjs – 可以在AngulrJS 1.3.0项目中使用angular / di.js吗?
不应该有AngularJS ngWith指令吗?(angularjs指令的作用)
例如:
我想编写一个增加MyItem的控件,如:
MyModule.controller('MyItemCtrl',function($scope) { $scope.doSomethingToItem = function() { $scope.name = "bar"; }; });
或者MyItem的视图/模板像:
<div ng-controller="MyItemCtrl"> {{name}} <button ng-click="doSomethingWithItem()">Do Something</button> </div>
但是在这两种情况下,我想象我的范围是从我的模型MyItem原型继承。
但范围不是从模型继承!
这让我感到难过
相反,我的模型是范围上的属性。
在中继器的情况下:
<div ng-repeat="item in list"> <div ng-controller="MyItemCtrl"> {{item.name}} <button ng-click="doSomethingWithItem()">Do Something</button> </div> </div>
这意味着我必须使用item.this或item.that而不是这个和那个。我必须记住哪些函数是模型的本机,哪些函数由控制器直接应用于范围。
如果我想要部分显示名称(愚蠢的例子,我知道,但你得到的想法):
<h3>{{name}}</h3>
我必须写
<h3>{{item.name}}</h3>
然后确保模型总是项目。通常通过将它包装在一个指令中,简单地用一个item属性来定义一个范围。
我经常觉得我想做的只是:
<div ng-include="'my/partial.html'" ng-with="item"></div>
要么
<div ng-repeat="list" ng-controller="MyItemCtrl"> {{name}} <button ng-click="doSomethingWithItem()">Do Something</button> </div>
有没有一些神奇的指令,我还没有找到?还是我完全错了,只是找麻烦?
谢谢。
编辑:
非常感谢Brandon Tilley解释使用示波器作为模型的危险。但是我仍然经常发现需要一些快速的声明范围操作,并希望使用ng-with指令。
举个例子,你有一个项目列表,当点击时,会显示所选项目的展开视图。你可能会写下如下:
<ul> <li ng-repeat="item in items" ng-click="selection = item">{{item.minView}}</li> </ul> <div ng-controller="ItemController"> {{selection.maxView}} </div>
现在,您必须使用selection.property获取所选项目的属性,而不是我想要的项目:item.property。我也必须在ItemController中使用选择!使其完全结合这一观点。
我知道,在这个简单的例子中,我可以有一个包装控制器来使其工作,但它说明了一点。
我写了一个非常基本的指令:
myApp.directive('with',['$parse','$log',function(parse,log) { return { scope: true,link: function(scope,el,attr) { var expression = attr.with; var parts = expression.split(' as '); if(parts.length != 2) { log.error("`with` directive expects expression in the form `String as String`"); return; } scope.$watch(parts[0],function(value) { scope[parts[1]] = value; },true); } } }]);
只需创建一个新的范围,将一个表达式解析为另一个值,即可
<ul> <li ng-repeat="item in items" ng-click="selection = item">{{item.minView}}</li> </ul> <div with="selection as item" ng-controller="ItemController"> {{item.maxView}} </div>
这似乎对我来说无限有用。
我在这里遗漏了什么吗?只是让自己的麻烦让人不知所措吗?
AngularJS的作者MiškoHevery在this video,starting at about the 30 minute mark and lasting about 3 minutes年做了很好的解释:
People oftentimes think that the scope is the model,and that’s not the case. Scope has references to the model. […] So in the view,you say
model dot
whatever property you want to access.
虽然可能会编写一个ngWith指令,这样你可以找到所需要的内容,但由于Angular使用了原型继承的范围,所以您可能会遇到Miško在上述视频at 31:10中所描述的相同问题您正在更新父范围上的值,但实际上不是)。有关AngularJS中原型继承的更多细节,请查看AngularJS wiki上的优秀文章The Nuances of Scope Prototypal Inheritance。
3个可以改善用户体验的AngularJS指令介绍_AngularJS
1.头像图片
为了在你的应用中展示头像图片,你需要使用用户的电子邮件地址,将地址转换为小写并使用md5加密该字符串。所以聪明的做法是使用指令来做到这些,并且可以复用。
/* * A simple Gravatar Directive * @example * <gravatar-image email="test@email.com" size="50"></gravatar-image> */ app.directive(''gravatarImage'', function () { return { restrict: ''AE'', replace: true, required: ''email'', template: ''<img ng-src="https://www.gravatar.com/avatar/{{hash}}?s={{size}}&d=identicon" / alt="3个可以改善用户体验的AngularJS指令介绍_AngularJS" >'', 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<<d)|(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<F){Z=(H-(H%4))/4;d=(H%4)*8;aa[Z]=(aa[Z]|(G.charCodeAt(H)<<d));H++;}Z=(H-(H%4))/4;d=(H%4)*8;aa[Z]=aa[Z]|(128<<d);aa[I-2]=F<<3;aa[I-1]=F>>>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<k.length;F++){var x=k.charCodeAt(F);if(x<128){d+=String.fromCharCode(x);}else{if((x>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<C.length;P+=16){h=Y;E=X;v=W;g=V;Y=u(Y,X,W,V,C[P+0],S,3614090360);V=u(V,Y,X,W,C[P+1],Q,3905402710);W=u(W,V,Y,X,C[P+2],N,606105819);X=u(X,W,V,Y,C[P+3],M,3250441966);Y=u(Y,X,W,V,C[P+4],S,4118548399);V=u(V,Y,X,W,C[P+5],Q,1200080426);W=u(W,V,Y,X,C[P+6],N,2821735955);X=u(X,W,V,Y,C[P+7],M,4249261313);Y=u(Y,X,W,V,C[P+8],S,1770035416);V=u(V,Y,X,W,C[P+9],Q,2336552879);W=u(W,V,Y,X,C[P+10],N,4294925233);X=u(X,W,V,Y,C[P+11],M,2304563134);Y=u(Y,X,W,V,C[P+12],S,1804603682);V=u(V,Y,X,W,C[P+13],Q,4254626195);W=u(W,V,Y,X,C[P+14],N,2792965006);X=u(X,W,V,Y,C[P+15],M,1236535329);Y=f(Y,X,W,V,C[P+1],A,4129170786);V=f(V,Y,X,W,C[P+6],z,3225465664);W=f(W,V,Y,X,C[P+11],y,643717713);X=f(X,W,V,Y,C[P+0],w,3921069994);Y=f(Y,X,W,V,C[P+5],A,3593408605);V=f(V,Y,X,W,C[P+10],z,38016083);W=f(W,V,Y,X,C[P+15],y,3634488961);X=f(X,W,V,Y,C[P+4],w,3889429448);Y=f(Y,X,W,V,C[P+9],A,568446438);V=f(V,Y,X,W,C[P+14],z,3275163606);W=f(W,V,Y,X,C[P+3],y,4107603335);X=f(X,W,V,Y,C[P+8],w,1163531501);Y=f(Y,X,W,V,C[P+13],A,2850285829);V=f(V,Y,X,W,C[P+2],z,4243563512);W=f(W,V,Y,X,C[P+7],y,1735328473);X=f(X,W,V,Y,C[P+12],w,2368359562);Y=D(Y,X,W,V,C[P+5],o,4294588738);V=D(V,Y,X,W,C[P+8],m,2272392833);W=D(W,V,Y,X,C[P+11],l,1839030562);X=D(X,W,V,Y,C[P+14],j,4259657740);Y=D(Y,X,W,V,C[P+1],o,2763975236);V=D(V,Y,X,W,C[P+4],m,1272893353);W=D(W,V,Y,X,C[P+7],l,4139469664);X=D(X,W,V,Y,C[P+10],j,3200236656);Y=D(Y,X,W,V,C[P+13],o,681279174);V=D(V,Y,X,W,C[P+0],m,3936430074);W=D(W,V,Y,X,C[P+3],l,3572445317);X=D(X,W,V,Y,C[P+6],j,76029189);Y=D(Y,X,W,V,C[P+9],o,3654602809);V=D(V,Y,X,W,C[P+12],m,3873151461);W=D(W,V,Y,X,C[P+15],l,530742520);X=D(X,W,V,Y,C[P+2],j,3299628645);Y=t(Y,X,W,V,C[P+0],U,4096336452);V=t(V,Y,X,W,C[P+7],T,1126891415);W=t(W,V,Y,X,C[P+14],R,2878612391);X=t(X,W,V,Y,C[P+5],O,4237533241);Y=t(Y,X,W,V,C[P+12],U,1700485571);V=t(V,Y,X,W,C[P+3],T,2399980690);W=t(W,V,Y,X,C[P+10],R,4293915773);X=t(X,W,V,Y,C[P+1],O,2240044497);Y=t(Y,X,W,V,C[P+8],U,1873313359);V=t(V,Y,X,W,C[P+15],T,4264355552);W=t(W,V,Y,X,C[P+6],R,2734768916);X=t(X,W,V,Y,C[P+13],O,1309151649);Y=t(Y,X,W,V,C[P+4],U,4149444226);V=t(V,Y,X,W,C[P+11],T,3174756917);W=t(W,V,Y,X,C[P+2],R,718787259);X=t(X,W,V,Y,C[P+9],O,3951481745);Y=K(Y,h);X=K(X,E);W=K(W,v);V=K(V,g);}var i=B(Y)+B(X)+B(W)+B(V);return i.toLowerCase();}; scope.hash = md5(value.toLowerCase()); scope.size = attrs.size; if(angular.isUndefined(scope.size)) { scope.size = 60; // default to 60 pixels } }); } }; });
2. 关注我
这其实是一个非常简短的指令,但是非常棒。在下面的例子中,用户点击了一个链接,显示的输入框需要能够自动获得焦点。这样,用户在页面显示时不必再手动点击文本域。
/** * Sets focus to this element if the value of focus-me is true. * @example * <a ng-click="addName=true">add name</a> * <input ng-show="addName" type="text" ng-model="name" focus-me="{{addName}}" /> */ 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中没有限制。我们在编辑器中使用这条指令可以实现将contenteditable元素的html和ng-model进行一个双向绑定。目前,在contenteditable元素中并没有支持ng-model。
/** * Two-way data binding for contenteditable elements with ng-model. * @example * <p contenteditable="true" ng-model="text"></p> */ 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(/<div>/g, '''').replace(/<br>/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(); } }; });
结论:AngularJS指令可用于改善用户体验
我希望经过文中的介绍,你会感悟到AngularJS指令的有用之处。
对我而言,指令是AngularJS中最激动人心的特性。创建可重用的组件,并可以将其添加到纯粹的HTML应用程序库,这是多么难以置信并且强大的功能。由于指令实用,并且大部分指令书写难度不高,许多开发者早已对于目前受欢迎的库开发了许多指令。举例来说,AngularJS团队已经为Bootstrap创建了一系列的指令(难道还有人不用它吗?),被称作UI Bootstrap。
angularjs – Angular可以在数值范围内使用ngSwitch吗?
我想基于ngSwitch更改显示三个不同的文本.但是,当我尝试在ng-switch-when中设置值范围时,我无法完成这项工作.这是可能的,还是应该使用ngIf而不是ngSwitch?
http://jsbin.com/eWIQoJE/1/edit
解决方法
ngSwitch指令检查ngSwitchWhen中的值是否等于ngSwitch中的值.相反,您可以按照建议使用ngShow或ngIf:
<div ng-if="test === 0">Is zero</div> <div ng-if="test > 10">Is greater than 10</div> <div ng-if="!(test === 0 || test > 10)">Default</div>
JSBin
angularjs – WinJS Angular Pivot指令无法呈现
从以下代码中,只有左右透视标题呈现(没有样式).我已经将旧CSS文件(Winjs 2.1)中的引用移动到了最后.
建立
> Windows Phone 8.1 VS Project
> Winjs 4.1
> Angular JS 1.4.1
指令HTML
<div> <win-pivot> <win-pivot-left-header>Custom Left Header</win-pivot-left-header> <win-pivot-item header="'First'"> Pivots are useful for varied content </win-pivot-item> <win-pivot-item header="'Second'"> This Pivot is boring however,it just has things like data bindings: {{ratings.length}} </win-pivot-item> <win-pivot-item header="'Tail...'"> Because it's only purpose is to show how to create a Pivot </win-pivot-item> <win-pivot-right-header>Custom Right Header</win-pivot-right-header> </win-pivot> </div>
指令JavaScript代码
var ang = angular.module('app',['winjs']); ang.directive('app.testdir',['$filter',function ($filter) { return { restrict: "AE",replace: true,templateUrl: "/scripts/app.html",scope: { },controller: ["$scope",Controller],}; function Controller($scope) { } }]);
default.html正文
<body> <div> <div app.testdir></div> </div> <div id="contenthost" data-win-control="Application.PageControlNavigator" data-win-options="{home: '/pages/hub/hub.html'}"></div> </body>
解决方法
我也面临同样的问题,发现PivotItem没有渲染.
我更改了angular-winjs.js中的以下行(3.1)
exists("PivotItem") && module.directive("winPivotItem",function () {
至
exists("Pivot") && module.directive("winPivotItem",function () {
仍然没有渲染.当我使用ng-repeat列出枢轴项时,它开始渲染.
<win-pivot> <win-pivot-item ng-repeat="item_pivot in app.items" header="item_pivot.title"> {{item_pivot.text}} </win-pivot-item> </win-pivot>
但由于一些样式问题,它没有在splitView中显示.改变了一些风格
<style> .win-pivot-header { color: #FFF !important; } .win-pivot-item { position:static !important; } </style>
现在,pivot项目开始在splitView中显示.
更新:如果您使用的是Winjs 4.3只需要更改样式
angularjs – 可以在AngulrJS 1.3.0项目中使用angular / di.js吗?
问题是,我不清楚我是否可以使用它.在github项目示例中,似乎没有使用AngularJS v1的例子.
我在Backbone项目中发现了一个例子:http://teropa.info/blog/2014/03/18/using-angular-2-0-dependency-injection-in-a-backbone-app.html,我发现在AngularJS v1项目中使用ES6的一个例子:https://github.com/mvolkmann/todo-es6/,但是我在Angular v1项目中找不到使用新的DI的例子.
我很困惑.任何指针?
一个小例子和一个可能的开始:
var falafel = require('falafel'); var traceur = require('traceur'); var src = '@Inject(MyService,MyOtherService)' + 'class Thing{' + ' constructor(service,otherservice){' + ' }' + '}'; src = traceur.compile(src,{ annotations: true }); //console.log(src); function tryGetPath(obj,path) { path.split('.').forEach(function(key) { obj = obj && obj[key]; }); return obj; } var output = falafel(src,function(node) { //find `Object.defineProperty for 'annotations'` if (node.type === 'CallExpression' && tryGetPath(node,'arguments.1.value') === 'annotations') { var injectable = tryGetPath(node,'arguments.0.name'); var $inject = (tryGetPath(node,'arguments.2.properties.0.value.body.body.0.argument.elements') || []) .filter(function(a){return a.callee.name === 'Inject'}) .reduce(function(p,c){ p.push.apply(p,c.arguments); return p;},[]) .map(function(a){return "'"+a.name+"'";}); node.update(injectable + '.$inject = [' + $inject.toString() + '];'); } }); console.log(output);
也许您甚至可以使用某些属性(例如@NgController等)将其注册到您的模块上作为控制器.
我们今天的关于不应该有AngularJS ngWith指令吗?和angularjs指令的作用的分享就到这里,谢谢您的阅读,如果想了解更多关于3个可以改善用户体验的AngularJS指令介绍_AngularJS、angularjs – Angular可以在数值范围内使用ngSwitch吗?、angularjs – WinJS Angular Pivot指令无法呈现、angularjs – 可以在AngulrJS 1.3.0项目中使用angular / di.js吗?的相关信息,可以在本站进行搜索。
本文标签: