Robert Brax
Robert Brax

Reputation: 7318

Directive improvement with watch and conditional templating

Here is the directive:

.directive("unloggedWarning", function () {

        return {
            restrict: "EA",
            link: function (scope) {
                scope.$watch('currentUser', function() {
                    if(scope.currentUser === null) {
                        scope.notLogged = true;

                    } else {
                        scope.notLogged = false;
                    }                });
            }
        };

    })

currentUser is rootscope persistant user current status with Parse backend. So whenever user logs out, the watch will set notLogged to true. I guess I can then in the html file view use conditonal ng-if to display warning when user unlogs.

How could I improve this directive, so from INSIDE the directive, I can conditionally inject a template with some html in it ? I can't seem to pass the log status from the if statement, to a standard directive "template: " parameter inside the directive.

Upvotes: 0

Views: 465

Answers (1)

Pieter Herroelen
Pieter Herroelen

Reputation: 6066

Here is a working example: http://plnkr.co/edit/S9fVZKXLx0Fc6QpkI8iH?p=preview

All I did was add the template:

 template: '<div><div ng-show="notLogged">You are not logged in</div></div>',

Upvotes: 1

Related Questions