Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular directive how to add an attribute to the element?

I'm wondering what's the way to do work this snippet:

//html <div ng-app="app">     <div ng-controller="AppCtrl">         <a my-dir ng-repeat="user in users">{{user.name}}</a>     </div> </div>  //js var app = angular.module('app', []); app.controller("AppCtrl", function ($scope) {     $scope.users = [{name:'John',id:1},{name:'anonymous'}];     $scope.fxn = function() {         alert('It works');     };  })   app.directive("myDir", function ($compile) {     return {         link:function(scope,el){             el.attr('ng-click','fxn()');             //$compile(el)(scope); with this the script go mad          }      }; }); 

I know it's about the compile phase but I don't get the point so a short explanation would be very appreciate.

like image 210
Whisher Avatar asked Feb 10 '14 21:02

Whisher


People also ask

How do you create an attribute directive?

Steps to create a custom attribute directiveCreate a class decorated with @Directive. Assign the attribute directive name to the selector metadata of @Directive decorator. Use ElementRef class to access DOM to change host element appearance and behavior.

What is attribute directive in Angular?

The attribute directive changes the appearance or behavior of a DOM element. These directives look like regular HTML attributes in templates. The ngModel directive which is used for two-way is an example of an attribute directive.

What are attribute directives meant for?

Attribute directives attach behavior to elements. An Attribute directive changes the appearance or behavior of a DOM element.

What is attribute in Angular?

Attribute binding in Angular helps you set values for attributes directly. With attribute binding, you can improve accessibility, style your application dynamically, and manage multiple CSS classes or styles simultaneously.


2 Answers

A directive which adds another directive to the same element:

Similar answers:

  • How to get ng-class with $dirty working in a directive?
  • creating a new directive with angularjs

Here is a plunker: http://plnkr.co/edit/ziU8d826WF6SwQllHHQq?p=preview

app.directive("myDir", function($compile) {   return {     priority:1001, // compiles first     terminal:true, // prevent lower priority directives to compile after it     compile: function(el) {       el.removeAttr('my-dir'); // necessary to avoid infinite compile loop       el.attr('ng-click', 'fxn()');       var fn = $compile(el);       return function(scope){         fn(scope);       };     }   }; }); 

Much cleaner solution - not to use ngClick at all:

A plunker: http://plnkr.co/edit/jY10enUVm31BwvLkDIAO?p=preview

app.directive("myDir", function($parse) {   return {     compile: function(tElm,tAttrs){       var exp = $parse('fxn()');       return function (scope,elm){         elm.bind('click',function(){           exp(scope);         });         };     }   }; }); 
like image 190
Ilan Frumer Avatar answered Sep 29 '22 02:09

Ilan Frumer


You can try this:

<div ng-app="app">     <div ng-controller="AppCtrl">         <a my-dir ng-repeat="user in users" ng-click="fxn()">{{user.name}}</a>     </div> </div>  <script> var app = angular.module('app', []);  function AppCtrl($scope) {         $scope.users = [{ name: 'John', id: 1 }, { name: 'anonymous' }];         $scope.fxn = function () {             alert('It works');         };     }  app.directive("myDir", function ($compile) {     return {         scope: {ngClick: '='}     }; }); </script> 
like image 45
Kaz-LA Avatar answered Sep 29 '22 04:09

Kaz-LA