为什么如果检查通过了?
我在检查是否$location.$$url
是!= 'dashboard'
但是,这种说法是真实的,但它仍然存在。这里 为什么如果检查通过了?
而过,我检查$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
打印出以下/dashboard
?
由于评论状态,您的缺失花括号。
尝试将其更改为以下内容,以使语句包含在条件中。
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
}
啊谢谢你的解释!我刚刚发现,你可以在没有'{}'的情况下编写JavaScript,并开始在几个地方改变它,我必须跳过该风格的单行要求。 7分钟! –
您可以在if
语句后面的一行内省略大括号({}
)。
您的代码应该是:
if ($location.$$url !== "/dashboard") {
console.log('Custome URL found!');
vs.customURL = true;
TagFactory.buildUrlObject($location.$$url);
}
而且,它一般不认为是最佳做法,以ommit正是因为这种情况大括号。
谢谢!是的,现在只会用于1行语句,试图优化到处,但要坚持可读性... –
我觉得@spender已经给出了答案,在大括号丢失:
if ($location.$$url !== "/dashboard") {
console.log('Custome URL found!');
vs.customURL = true;
TagFactory.buildUrlObject($location.$$url);
}
你能向我们提供您的文字代码格式? –
您的缩进似乎表明您希望随后的语句由'if'语句来管理,但您没有任何大括号。也许这只是马虎的格式。要清楚,Javascript!= Python – spender
要更清楚你期望(不)发生什么。张贴实际的代码不是截图。从我的截图中可以看出,条件未执行(我没有看到“Custome URL found!”控制台日志)。也许你的意思是在执行之后的一些代码?也许你需要将代码封装在'{...}'中作为'if'块的一部分?更清楚。 –