将字符串保存到变量和console.log时,拆分字符串会得到不同的结果
Splitting a string gives different result when saved to variable and when console.log-ing it
我有一个非常简单的函数,它在$location
发生变化时执行(见下文)。问题是,当$location.path() == "/browser"
时,分配中出现的$location.path().split("/")
返回["browser"]
,但直接在console.log
内部运行会返回["", "browser"]
。如何合理地解释这种差异?
angular.module("blah", [])
.controller("navigation", function($scope, $location) {
var updatePage = function() {
var temp = $location.path().split("/");
console.log($location.path(), $location.path().split("/"), temp);
$scope.page = temp.splice(0,1)[0];
$scope.args = temp;
//console.log($scope.page);
};
$scope.changePage = function(path) {
if (path.match("^https?:")) {
window.location = path;
} else {
$location.path(path);
}
};
$scope.args = [];
$scope.$on("$locationChangeSuccess", function(event, data) {
updatePage();
});
updatePage();
});
您可能会看到类似以下代码的"问题":
var temp = $location.path().split("/");
$scope.page = temp.splice(0,1)[0];
...
console.log($location.path(), $location.path().split("/"), temp);
在记录时,temp
已经splice
dsplice
(与slice
不同)也从原始阵列中移除了elment。
更新:
另一个可能的原因是:
Firefox(与Chrome不同)在记录时不会记录对象的值,而是对对象的
引用(数组也是对象)。因此,阵列的任何后续修改(例如拼接)也会影响记录的对象/阵列。
如果您改为记录JSON.stringify(temp)
,您应该会看到预期的结果。
相关文章:
- Regex模式匹配,从Javascript中的字符串中提取时间,结果出乎意料
- 将敲除绑定应用于模板,并将结果作为字符串获取
- 将Javascript Regex结果转换为字符串(?)
- Geoplugin字符串结果为德语单词/语言
- Javascript:拆分字符串,但仅使用 1 个结果
- 解析JSON字符串返回未定义的结果
- 在谷歌地图API自动完成中显示不匹配字符串的结果
- 将字符串结果返回到表单元格中的新行中
- mongodb-mongoose-find-如何获得适用的字符串作为结果
- 自定义查询字符串分析器返回不正确的结果
- 如何从数据库中获取JSON字符串结果以供以后使用
- 使用$se.trustAsHtml呈现字符串返回未定义的结果
- 调用.ajax方法得到未定义的json结果,返回json格式列表<字符串>
- 如何获取控制台.log输出 getter 结果而不是字符串“[Getter/Setter]”
- getElementById 给出结果和字符串
- Json字符串结果计数与JavaScript
- 使用AngularJs显示标签中函数的字符串结果
- 我可以调用什么来过滤字符串结果?
- 无法获取replaceWith的字符串结果
- 将字符串/结果从JS发送到HTML页面