Load angular directive in view, based on $scope value

Willem Mulder

I have a directive defined as

Application.Directives.directive('listview', function() {
    return {
        restrict: 'EAC',
        templateUrl: 'directives/listview/view.html'
    };
});

And then want to include it from the main view like this

<div class="{{directiveName}}">
</div>

where directiveName equals "listview". However, it does not work. It generates the below code, but the listview directive does not get loaded

<div class="listview">
</div>

Yet, when I type the above generated code directly into the main template, it does load the directive. How come? How can I make it work?

Willem Mulder

So I found a way. What you'd want is something like this

<div {{directiveNameInScope}}></div>

But again, that doesn't work. So I created a directive to do it for you. It works like

<div loaddirective="directiveNameInScope"></div>

where the loaddirective directive looks like

Application.Directives.directive('loaddirective', function($compile) {
    return {
        restrict: 'A',
        scope: { loaddirective : "=loaddirective" },
        link: function($scope, $element, $attr) {
            var value = $scope.loaddirective;
            if (value) {
                // Load the directive and make it reactive
                $element.html("<div "+value+"></div>");
                $compile($element.contents())($scope);
            }
        },
        replace: true
    };
});

I put it up on github here: https://github.com/willemmulder/loaddirective

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

angular-ui view nested in a directive does not load view content

From Dev

angular-ui view nested in a directive does not load view content

From Dev

Angular - changes to directive controller's scope aren't reflected in view

From Dev

Angular directive with scope : @ not working

From Dev

Angular Directive scope undefined

From Dev

Updating $scope in an Angular directive

From Dev

" =? " in Angular Directive Isolate Scope

From Dev

angular directive scope missunderstanding

From Dev

Angular directive modifying scope

From Dev

Angular directive with scope : @ not working

From Dev

Change a scope value in directive

From Dev

How to change ng-model scope value in custom Angular directive?

From Dev

How to get updated value for isolated scope using angular directive?

From Dev

Angular Directive Load Order

From Dev

Angular directive: multiple values from directive to scope

From Dev

Angular directive: multiple values from directive to scope

From Dev

Pass angular directive scope to directive controller

From Dev

Angular Directive using '=' scope & controllerAs

From Dev

Angular scope issue in a Controller and a Directive

From Dev

Angular pass dynamic scope to directive

From Dev

Testing angular directive scope method

From Dev

Angular2 Directive with scope

From Dev

scope of event arguments in angular directive

From Dev

Angular Directive cannot access scope

From Dev

Angular directive - what if scope is not set?

From Dev

Angular bind directive scope dynamically

From Dev

Angular directive controller scope visibility

From Dev

Angular pass dynamic scope to directive

From Dev

Angular directive without scope defined

Related Related

HotTag

Archive