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函数