"$location#search()"调用的第一个参数必须是字符串或对象



我在 angularjs 和 MVC 应用程序上工作,我的应用程序运行良好,但我的控制台中出现错误。

错误: $location:isrcharg

错误的 $location.search(( 参数类型

$location#search()调用的第一个参数必须是字符串或对象

这是我的AngularJS导航服务:

self.goBack = function () {
$window.history.back();
}
self.navigateTo = function (path, params) {
if (params === null) {
$location.path(MyApp.rootPath + path);
}
else {
$location.path(MyApp.rootPath + path).search(params);
}
};
self.refreshPage = function (path) {
$window.location.href = MyApp.rootPath + path;
};
self.clone = function (obj) {
return JSON.parse(JSON.stringify(obj))
};
self.querystring = function (param) {
if ($location.search !== null)
return $location.search()[param];
else
return null;
};
self.resetQueryParams = function () {
$location.url($location.path());
};
return this;
};

上面的代码是一个常见的工厂,所以在将其注入我的控制器后,这就是我的导航方式 在角度我使用viewModelHelper.navigateTo("/home");然后,如果我想导航到 MVC 页面,我使用

viewModelHelper.refreshPage("index");

im 导航到

$location.path(MyApp.rootPath + path).search(params);

我的参数没有定义。如何解决这个未定义的问题

self.navigateTo = function (path, params) {
if ( ̶p̶a̶r̶a̶m̶s̶ ̶=̶=̶=̶ ̶n̶u̶l̶l̶  !params) {
$location.path(MyApp.rootPath + path);
}
else {
$location.path(MyApp.rootPath + path).search(params);
}
};

self.navigateTo = function (path, params) {
if (params) {
$location.path(MyApp.rootPath + path).search(params);
}
else {
$location.path(MyApp.rootPath + path);
}
};

在上面的示例中,仅当params变量为 true 时,代码才会执行.search方法。

在 JavaScript 中,真实值是在布尔上下文中评估时被视为true的值。所有值都是真值,除非它们被定义为假值(即,除了false0""nullundefinedNaN(。

有关更多信息,请参阅 MDN JavaScript 参考 - truthy

相关内容

最新更新