Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 使用监视和条件模板进行指令改进_Angularjs - Fatal编程技术网

Angularjs 使用监视和条件模板进行指令改进

Angularjs 使用监视和条件模板进行指令改进,angularjs,Angularjs,以下是指令: .directive("unloggedWarning", function () { return { restrict: "EA", link: function (scope) { scope.$watch('currentUser', function() { if(scope.currentUser === null) {

以下是指令:

.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是具有分析后端的rootscope持久用户当前状态。因此,每当用户注销时,手表都会将notLogged设置为true。我想我可以在html文件视图中使用conditonal ng if在用户卸载时显示警告


如何改进该指令,以便从指令内部有条件地向模板中注入一些html?我似乎无法将日志状态从if语句传递到指令内的标准指令“template:”参数。

以下是一个工作示例:

我所做的只是添加模板:

 template: '<div><div ng-show="notLogged">You are not logged in</div></div>',
模板:“您未登录”,

旁注:
如果条件为a=true,否则a=false
可以(应该?)简化为
a=condition
。如果要显示警告消息,请单击注销?在这种情况下,您可以直接编写一个ng click函数