directive

Angular Directive Different Template

社会主义新天地 提交于 2019-11-28 03:58:16
I have a directive myDirective with variable type. If I run <my-directive type="X"> I want the directive to use templateUrl: x-template.html. If I do <my-directive type="Y"> I want the directive to use templateUrl: y-template.html. This is my current directive. app.directive('myDirective', function() { var myDirective = { templateUrl: 'X-template.html', restrict: 'E', scope: { type: '=' }, }; return myDirective; }); I read thru stackoverflow and angular documentation but have not found anything that I need. I am now trying to do something along the lines of: if ($scope.type === 'X') {

AngularJS - Attribute directive input value change

ぐ巨炮叔叔 提交于 2019-11-28 03:49:04
I've got an AngularJS attribute directive, and I would like to take an action any time its parent input's value changes. Right now I'm doing it with jQuery: angular.module("myDirective", []) .directive("myDirective", function() { return { restrict: "A", scope: { myDirective: "=myDirective" }, link: function(scope, element, attrs) { element.keypress(function() { // do stuff }); } }; }); Is there a way to do this without jQuery? I'm finding the keyPress event isn't doing exactly what I want it to, and while I'm sure I'll come up with a solution, I get a little nervous when I resort to using

Prevent `ng-include` from creating isolated scope. AngularJS

本小妞迷上赌 提交于 2019-11-28 00:27:40
问题 I am using ng-include src directive at multiple places in my HTML. Each ng-include is creating an isolated scope for itself which apparently is causing a lot of trouble for me. For example. <div ng-controller="parent"> <div ng-include src="'id1'"> </div> <div ng-include src="'id2'"> </div> <div ng-include src="'id2'"> </div> </div> In the html mentioned above, 4 scopes are getting created. 1 at parent where controller is defined and 3 are getting created by default at each ng-include src . Is

How to add validation attributes in an angularjs directive

心已入冬 提交于 2019-11-27 23:42:10
问题 I am trying to write an angular directive that adds validation attributes to the tag, but it doesn't seem to be working. Here is my demo. You will notice that "Is Valid" remains true if you delete the text in the second input box, but goes to false if you delete the text in the first input box. http://plnkr.co/edit/Rr81dGOd2Zvio1cLYW8D?p=preview Here is my directive: angular.module('demo', []) .directive('metaValidate', function () { return { restrict: 'A', link: function (scope, element,

AngularJS element directives not displaying when using self-closing tags

早过忘川 提交于 2019-11-27 23:33:01
I have in my html file directives <add /> <back /> and the directives are on the form .directive('add', ['$window', ... and .directive('back', ['$window', This works fine. If i change the directives to camel case: .directive('addPlayer', ['$window', ... <add_player /> <back /> and <add:player /> <back /> display fine whereas <add-player /> regular dash <back /> displays only <add-player> and everything after is not displayed. Any ideas why? EDIT: I've kind of gotten the same behaviour here http://plnkr.co/edit/cpP4c2TyZwv5Y4BrNUBb?p=preview To lay your question to rest, I am quoting the

Attribute directive with ngModel to change field value

做~自己de王妃 提交于 2019-11-27 20:02:19
问题 I want to change (force) input field values while typing using a attribute Directive. With it I would like to create directives like uppercase, lowercase, maxlength, filterchar, etc. to be used on input fields on forms. I found this example: Angular 2 Attribute Directive Typescript Example but this doesn't seem to work. Maybe it did for an earlier build of Angular2. It is however exactly what I would like to do. When I create a directive like this: import {Directive} from 'angular2/core';

What are the benefits of a directive template function in Angularjs?

梦想的初衷 提交于 2019-11-27 17:59:46
According to the documentation a template can be a function which takes two parameters, an element and attributes and returns a string value representing the template. It replaces the current element with the contents of the HTML. The replacement process migrates all of the attributes and classes from the old element to the new one. The compile function deals with transforming the template DOM. It takes three parameters, an element , attributes and transclude function. The transclude parameter has been deprecated. It returns a link function. It appears that a template and a compile functions

StackOverFlow精彩问答赏析:有jQuery背景的开发者如何建立起AngularJS的思维

半城伤御伤魂 提交于 2019-11-27 16:52:42
【编辑注】本文来自StackOverFlow上How do I “think in AngularJS” if I have a jQuery background?一题中得票最高的回答。该回答得票超过3000次,回答者Josh David Miller是活跃于开源社区的开发者,也是Emergenesis公司的联合创始人。该答案最初由数云架构师韩铮翻译并发布在 自己的博客 上,在征得Josh同意后由韩铮本人推荐给 InfoQ进行分享,并在经过InfoQ社区编辑崔康审校后发布在此。 1. 不要先设计页面,然后再使用DOM操作来改变它的展现 在jQuery中,你通常会设计一个页面,然后再给它动态效果。这是因为jQuery的设计就是为了扩充DOM并在这个简单的前提下疯狂的生长的。 但是在AngularJS里,必须从头开始就在头脑中思考架构。必须从你想要完成的功能开始,然后设计应用程序,最后来设计视图,而非“我有这么一个DOM片段,我想让他可以实现XXX效果”。 2. 不要用AngularJS来加强jQuery 类似的,不要以这样的思维开始:用jQuery来做X,Y和Z,然后只需要把AngularJS的models和controllers加在这上面。这在刚开始的时候显得非常诱人,这也是为什么我总是建议AngularJS的新手完全不使用jQuery,至少不要在习惯使用“Angular Way

Angular directive with ng-repeat, ng-show “Show more” and lazy-load

三世轮回 提交于 2019-11-27 14:57:07
问题 I use this directive, iterating over an array "myArr", filtering for a few conditions. <myDirective myData='item' ng-repeat="item in filteredArr = (myArr | filter:searchField | filter:checkboxFilter)" ng-show="$index < visible" /> This gives me two issues I'd like to get some input on: a) the ng-show part is there because I have a condition that handles this: <p> <a ng-click='visible=visible+10' ng-show='visible < filteredArr.length'>Show more</a> </p> in order to show or hide the "Show more"

Unit testing a directive that defines a controller in AngularJS

≡放荡痞女 提交于 2019-11-27 13:46:32
问题 I'm trying to test a directive using Karma and Jasmine that does a couple of things. First being that it uses a templateUrl and second that it defines a controller. This may not be the correct terminology, but it creates a controller in its declaration. The Angular application is set up so that each unit is contained within its own module. For example, all directives are included within module app.directive, all controllers are contained within app.controller, and all services are contained