如果检查通过,为什么这样做?

时间:2015-06-19 14:37:57

标签: javascript angularjs if-statement

我正在检查$location.$$url是否为!= 'dashboard'但是这句话是正确的,但它会继续。

// the URL is currently at /dashboard
if ($location.$$url !== "/dashboard") 
    console.log('Custome URL found!');
    vs.customURL = true;
    TagFactory.buildUrlObject($location.$$url);

您可以在下方看到console.log打印出/dashboardenter image description here

我也在这里检查$location$$url"/dashboard"所以应该跳过if语句,但它会继续吗? enter image description here

3 个答案:

答案 0 :(得分:2)

您可以在{}语句后仅为一行省略花括号(if)。
你的代码应该是:

if ($location.$$url !== "/dashboard") {
    console.log('Custome URL found!');
    vs.customURL = true;
    TagFactory.buildUrlObject($location.$$url);
}

此外,正是因为这种情况,通常不认为最好不要使用大括号。

答案 1 :(得分:2)

正如评论所述,你缺少大括号。

尝试将其更改为以下内容,以便语句包含在条件中。

if ($location.$$url !== "/dashboard") {
    console.log('Custome URL found!');
    vs.customURL = true;
    TagFactory.buildUrlObject($location.$$url);
}

如果没有花括号,则if语句是单个语句。例如,您可以这样做:

if ($location.$$url !== "/dashboard") alert("not dashboard");

或者

if ($location.$$url !== "/dashboard"){
   alert("not dashboard");
   //Additional statements here
}

答案 2 :(得分:1)

我认为@spender已经给出了答案,大括号丢失了:

if ($location.$$url !== "/dashboard") {
    console.log('Custome URL found!');
    vs.customURL = true;
    TagFactory.buildUrlObject($location.$$url);
}
相关问题