'angularjs custom directive not working
I added my custom attribute to my application like this
'use strict';
myApp.directive('orientable', function () {
return {
restrict: 'A',
link: function (scope, element, attrs, controller) {
element.bind("load", function (e) {
console.log('test');
/* my code is here*/
});
}
}
});
Add using this in my view like this
<div class="xyz" orientable></div>
but it is not calling link function, what I am doing wrong.
Solution 1:[1]
Your fiddle doesn't work.
Don't know what you are trying to do, but this one works.
Just do what you want on link
function.
Solution 2:[2]
as lante said, your fiddle is broken. It isn't loading angular or its dependecies (cookies and animation). On another note, there is no load
for a div. Just move your log out of the element.bind...
var myApp = angular.module('myApp', []);
myApp.controller('myctrl', ['$scope','$rootScope',function($scope,$rootScope)
{
//controller logic
}]);
myApp.directive('orientable',function () {
return {
restrict:'A',
link: function(scope, element, attrs, controller) {
console.log('yahan');
element.addClass("vertical");
}
}
});
You also had restrict: 'E'
which is element name as in <orientable></orientable>
You have it as an attribute so you need to change the restrict
to A
You can do restrict: 'EA'
and have the ability to use either method (attribute or element name).
Solution 3:[3]
I have faced same problem. Finally I got to know that we need to load "orientable" as DI module for ng-app. We need to add this "orientable".
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | lante |
Solution 2 | Ronnie |
Solution 3 | Paresh Mayani |