为什么如果检查通过了?

问题描述:

我在检查是否$location.$$url!= 'dashboard'但是,这种说法是真实的,但它仍然存在。这里 enter image description here为什么如果检查通过了?

而过,我检查$location$$url"/dashboard"所以if声明应被忽略,但它继续:

// 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

+1

你能向我们提供您的文字代码格式? –

+1

您的缩进似乎表明您希望随后的语句由'if'语句来管理,但您没有任何大括号。也许这只是马虎的格式。要清楚,Javascript!= Python – spender

+0

要更清楚你期望(不)发生什么。张贴实际的代码不是截图。从我的截图中可以看出,条件未执行(我没有看到“Custome URL found!”控制台日志)。也许你的意思是在执行之后的一些代码?也许你需要将代码封装在'{...}'中作为'if'块的一部分?更清楚。 –

由于评论状态,您的缺失花括号。

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

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 
} 
+1

啊谢谢你的解释!我刚刚发现,你可以在没有'{}'的情况下编写JavaScript,并开始在几个地方改变它,我必须跳过该风格的单行要求。 7分钟! –

您可以在if语句后面的一行内省略大括号({})。
您的代码应该是:

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

而且,它一般不认为是最佳做法,以ommit正是因为这种情况大括号。

+0

谢谢!是的,现在只会用于1行语句,试图优化到处,但要坚持可读性... –

我觉得@spender已经给出了答案,在大括号丢失:

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