1

I have something like this -

<input type="text" value="" title="Title"> ...

How can I add my custom directive as attribute to the existing html element on the page, let's say - directive as categoryLookup -

<input type="text" value="" title="Title" category-lookup> ...

I need to do this dynamically on page load, i.e make the input text behave per directive logic.

Thanks in advance.

2
  • It's not really clear what you are trying to accomplish here. You can't "dynamically add" angular directives to already rendered DOM elements without recompiling the DOM, angular will never be able to load them properly. Commented Sep 24, 2015 at 15:16
  • "Dynamically add attribute on page load". Is there a specific reason why you want to do this? The Angular way would be to simply add category-lookup to the input tag in HTML template and define your directive to bring it to life. Commented Sep 24, 2015 at 17:39

1 Answer 1

2

You can do this with leveraging $compile after targeting your element. I added an id and used vanilla JS in this example, but you may have more at your disposal e.g. jQuery. After you have your element, just $compile it in your associated controller. Observe the following example...

<div ng-app="app" ng-controller="ctrl">
    <input id="myInput" type="text" title="Title"> 
</div>

angular.module('app', [])
.controller('ctrl', function($scope, $compile) {
    $compile(angular.element(document.getElementById('myInput')).attr('category-lookup', 'category-lookup'))($scope)
})
.directive('categoryLookup', function() {
    return {
        restrict: 'A',
        link: function(scope, elem, attrs) {
            console.log('directive registered')
        }
    }
});

JSFiddle Link - demo


Also, "dynamically on page load" can mean a few things. I assumed you are in a fully fleshed out AngularJS ecosystem, but there does exist the notion that you do not even have a controller and need to somehow cherrypick this directive and compile it on "load". Here is a nitty gritty example how you can do so, though, it's generally regarded as a bad practice, and instead leveraging the above logic in an associated controller is preferable. Observe the following...

<div ng-app="app">
    <input id="myInput" type="text" value="" title="Title"> 
</div>

angular.element(document).ready(function () {

    var $injector = angular.injector(['ng', 'app']);

    $injector.invoke(function($rootScope, $compile) {
        $compile(angular.element(document.getElementById('myInput')).attr('category-lookup', 'category-lookup'))($rootScope)
    });
});

JSFiddle Link - demo - no controller

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, $compile is what did the trick. demo 1 worked for me. I injected '$compile' into the directive to the compile the html template. Thank you very much :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.