在这篇文章中,我们将带领您了解reloadOnSearch为false时,$location.search上的AngularJs$watch无法正常工作的全貌,包括table.reload无效的相关情
在这篇文章中,我们将带领您了解reloadOnSearch为false时,$ location.search上的AngularJs $ watch无法正常工作的全貌,包括table.reload无效的相关情况。同时,我们还将为您介绍有关AngularJS $location.path(‘/’)不清除search()参数、angularjs – $location.path无法从http拦截器工作、angularjs – Angular2 $location.search()等价物(设置查询参数)、angularjs – 与$location.search():如何从url当它为null时删除参数的知识,以帮助您更好地理解这个主题。
本文目录一览:- reloadOnSearch为false时,$ location.search上的AngularJs $ watch无法正常工作(table.reload无效)
- AngularJS $location.path(‘/’)不清除search()参数
- angularjs – $location.path无法从http拦截器工作
- angularjs – Angular2 $location.search()等价物(设置查询参数)
- angularjs – 与$location.search():如何从url当它为null时删除参数
reloadOnSearch为false时,$ location.search上的AngularJs $ watch无法正常工作(table.reload无效)
我 routeProvider 路由具有 reloadOnSearch 设置为 假 :
$routeProvider .when( "/film/list", { templateUrl: ''/film/list.html'', controller: FilmListController, reloadOnSearch: false } .... )
我这样做是因为我不希望在查询字符串更改后重新加载整个控制器,但是我仍然使用它,并且url看起来像这样:film/list?sort=isbn&order=asc&offset=0
。现在,每当查询字符串更改时,我都想从控制器中调用一个函数。所以我试图在控制器中设置一个
$ watch :
$scope.location = $location;$scope.$watch( ''location.search()'', function( search) { $scope.list(); });
但这是行不通的。如果我将 $ watch 设置 为监视 查询字符串的各个参数的更改,则它将起作用。但是我不想为查询字符串的每个参数设置$
watch。我现在使用的解决方案是这样的:
$scope.location = $location;$scope.$watch( ''location.url()'', function( url ) { if($location.path() == ''/film/list'') $scope.list(); });
因此,我没有关注搜索,而是关注整个URL,然后检查该路径是否是我在 routeProvider中
设置的有条件调用该函数的路径。我对此解决方案不满意的是,我必须明确键入要匹配的$ location.path的值。
任何人都可以提出更好的解决方案,也许可以向我解释为什么 $ watch 对于 $ location.search() 不起作用?
答案1
小编典典您可以$routeUpdate
在控制器中监听事件:
$scope.$on(''$routeUpdate'', function(){ $scope.sort = $location.search().sort; $scope.order = $location.search().order; $scope.offset = $location.search().offset;});
AngularJS $location.path(‘/’)不清除search()参数
$location.path('/login');
如果用户未登录,则重定向回登录页面,或者通常使用此方法在任何地方重定向.但是,如果我已经有一个看起来像这样的网址
/注册/最后的步?标记= mF6xY2cQvB9Vccb0J1l5uTu4H10lWkkf
并重定向到
$location.path( ‘/’);
那么搜索参数不会被清除,所以我得到这样的网址
/?标记= mF6xY2cQvB9Vccb0J1l5uTu4H10lWkkf
即使我使用UI-Router
$state.go('home');
该
?令牌= mF6xY2cQvB9Vccb0J1l5uTu4H10lWkkf
部分仍在那里.
我不明白.每次我需要去另一个州或网址时,我是否真的需要手动清除参数?这似乎不太好
我是合法的.我做错了什么?我不想做window.location.href,因为这会使整页重新加载,这不是我想做的事情.
我该怎么做才能清除参数?我一定做错了什么.
解决方法
你可以像这样使用它(是的,它的用途不仅仅是搜索…但在这个上下文中它的名字也是如此).
// Get the current value var curr_search = $location.search(); // Clear the current search $location.search({}); // Set the current search values $location.search({key: "value"});
angularjs – $location.path无法从http拦截器工作
function($location,$q) { return { request: function (config) { return config || $q.when(config); },requestError: function(request){ return $q.reject(request); },response: function (response) { return response || $q.when(response); },responseError: function (response) { if (response && response.status === 401) { $location.path('/login'); } return $q.reject(response); } }; }
问题是$location.path(‘/ login’)调用似乎不起作用,路径被完全忽略,我仍然在我的会话到期时我所在的路径(尽管是一个空白页面).一种可能的解决方法是使用传统的window.location =’/#!/ login’;但我觉得这不太理想.我还发现如果我删除返回$q.reject(响应);我得到它的工作,但这引入了另一个错误,其中无法找到响应的数据属性(此处讨论的解决方案:Angular.js $http.post TypeError: Cannot read property ‘data’ of undefined).
我正在跑角1.2.21.
关于可能会发生什么的任何想法?回到window.location不是世界的尽头,但我喜欢这个神秘的一个很好的整洁解决方案:).
提前致谢.
解决方法
我不确定,但看起来$location.path(…)不会改变位置,因为应用程序等待直到所有的promises将被解析,并且拦截器内的promise被拒绝(返回$q.reject(response);)
在我的情况下,执行http请求的服务放在UI路由器状态的resolve部分内,因此我可以使用$stateChangeError事件来处理错误.
(对于ngRoute使用$routeChangeError事件)
希望这可以帮到你 :)
NB我的应用程序的后端返回401响应正文中的自定义错误消息
PS必须有一些更好的解决方案
更新:
更普遍的解决方法:
module.config中的拦截器
... responseError: function (response) { if (response && response.status === 401) { $rootScope.$emit('loginrequired'); } return $q.reject(response); } ...
和事件处理程序在module.run部分的某处
$rootScope.$on('loginrequired',function() { $location.path('/login'); });
(我用$state.go(‘login’)测试了处理程序,但$location.path(‘/ login’);也应该工作:))
angularjs – Angular2 $location.search()等价物(设置查询参数)
在angular1中,我使用$location.search(name,value)来简单地在变化时设置seach参数.现在我想在angular2中得到类似的东西.
还是错了?
解决方法
import {Component} from 'angular2/core'; import {RouteConfig,ROUTER_DIRECTIVES} from 'angular2/router'; import {ProfileComponent} from './profile.component'; @Component({ selector: 'my-app',template: ` <router-outlet></router-outlet> `,directives: [ROUTER_DIRECTIVES] }) @RouteConfig([ {path: '/profile/:id',name: 'Profile',component: ProfileComponent} ]) export class AppComponent { }
The :id is a route parameter and you can do an URL like that:
/profile/2 and 2 is the value of the id parameter.
您可以在Angular2 doc:router doc中找到更多详细信息
angularjs – 与$location.search():如何从url当它为null时删除参数
传递给search()的搜索参数由表单设置。选择和取消选择元素会导致一些参数值为“null”,因此网址如下所示:
myapp.com/search?type=text¶meter=null
所以我想从URL中删除那些“null”参数。在文档中,有一个“paramValue”,它可以作为第二个参数传递给.search(search,paramValue):如果值为null,则该参数将被删除。
但我不能让这个工作…任何建议?
编辑:这里是一个基于@BKM解释的解决方案
要删除每个为null的参数,需要遍历所有的参数并测试每个参数,如下所示:
for (var i in search) { if (!search[i]) $location.search(i,null); } $location.search(search);
在你的参数为null的情况下,在你的case’参数’,你可以通过指定一个空值来删除它从url;
$location.search('parameter',null);
希望它有帮助。
我们今天的关于reloadOnSearch为false时,$ location.search上的AngularJs $ watch无法正常工作和table.reload无效的分享已经告一段落,感谢您的关注,如果您想了解更多关于AngularJS $location.path(‘/’)不清除search()参数、angularjs – $location.path无法从http拦截器工作、angularjs – Angular2 $location.search()等价物(设置查询参数)、angularjs – 与$location.search():如何从url当它为null时删除参数的相关信息,请在本站查询。
本文标签: