Angularjs 不工作时的角度开关
当我的Angular模板中的语句和switch语句输出必须是“您是用户”时,我尝试进行一个简单的切换,但它表示我是一位来宾 我的代码有什么问题Angularjs 不工作时的角度开关,angularjs,switch-statement,directive,Angularjs,Switch Statement,Directive,当我的Angular模板中的语句和switch语句输出必须是“您是用户”时,我尝试进行一个简单的切换,但它表示我是一位来宾 我的代码有什么问题 <p><strong>Current role</strong></p> <p>{{ app.currentUser.role }}</p> <p><strong>Role to compare with</strong></p> &l
<p><strong>Current role</strong></p>
<p>{{ app.currentUser.role }}</p>
<p><strong>Role to compare with</strong></p>
<p>{{ app.userRoles.user }}</p>
开关语句
角度控制器:
Current role
user
Role to compare with
user
You're a guest.
function MainController() {
my = this;
my.currentUser = { role : 'user'};
my.userRoles = { user : 'user' };
}
Plunker链接:
根据
ngSwitch
的文档:
请注意,要匹配的属性值不能是表达式。它们被解释为要匹配的文本字符串值。例如,ng开关when=“someVal”
将匹配字符串“someVal”
而不是表达式$scope.someVal
的值
看起来您将无法与当前的app.userRoles.*
进行匹配。如果您使用以下内容:
<div ng-switch-when="user">You're a user.</div>
您是一个用户。
你应该会得到想要的结果
来源:根据
ngSwitch
的文档:
请注意,要匹配的属性值不能是表达式。它们被解释为要匹配的文本字符串值。例如,ng开关when=“someVal”
将匹配字符串“someVal”
而不是表达式$scope.someVal
的值
看起来您将无法与当前的app.userRoles.*
进行匹配。如果您使用以下内容:
<div ng-switch-when="user">You're a user.</div>
您是一个用户。
你应该会得到想要的结果
来源:您的模板应该如下所示
<div ng-switch on="app.currentUser.role">
<div ng-switch-when="admin">You're admin.</div>
<div ng-switch-when="moderator">You're moderator.</div>
<div ng-switch-when="user">You're a user.</div>
<div ng-switch-default>You're a guest.</div>
</div>
你是管理员。
你是主持人。
你是一个用户。
你是客人。
您的模板应如下所示
<div ng-switch on="app.currentUser.role">
<div ng-switch-when="admin">You're admin.</div>
<div ng-switch-when="moderator">You're moderator.</div>
<div ng-switch-when="user">You're a user.</div>
<div ng-switch-default>You're a guest.</div>
</div>
你是管理员。
你是主持人。
你是一个用户。
你是客人。
您不需要使用任何switch语句
<div ng-if="app.currentUser.role">
<div>You're {{app.currentUser.role}}.</div>
</div>
<div ng-if="!app.currentUser.role">
<div>You're a guest.</div>
</div>
您是{{app.currentUser.role}。
你是客人。
您不需要使用任何switch语句
<div ng-if="app.currentUser.role">
<div>You're {{app.currentUser.role}}.</div>
</div>
<div ng-if="!app.currentUser.role">
<div>You're a guest.</div>
</div>
您是{{app.currentUser.role}。
你是客人。