AngularJS $location 不改变路径 [英] AngularJS $location not changing the path

查看:32
本文介绍了AngularJS $location 不改变路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在提交表单后更改页面 URL 时遇到问题.

这是我的应用程序的流程:

  1. 设置了路由,URL 被识别到某个表单页面.
  2. 页面加载、控制器设置变量、指令被触发.
  3. 触发一个特殊的表单指令,该指令使用 AJAX 执行特殊的表单提交.
  4. 在执行 AJAX 之后(Angular 不处理 AJAX)然后会触发一个回调,并且指令调用设置位置的 $scope.onAfterSubmit 函数.

问题是设置位置后什么也没有发生.我也尝试将位置参数设置为 / ......不.我也试过不提交表格.没有任何效果.

我已经测试过代码是否到达了 onAfterSubmit 函数(确实如此).

我唯一的想法是函数的范围以某种方式发生了变化(因为它是从指令调用的),但是如果范围发生变化,它又如何调用 onAfterSubmit ?

这是我的代码

var Ctrl = function($scope, $location, $http) {$http.get('/resources/' + $params.id + '/edit.json').success(function(data) {$scope.resource = 数据;});$scope.onAfterSubmit = function() {$location.path('/').replace();};}Ctrl.$inject = ['$scope','$location','$http'];

有人可以帮我吗?

解决方案

几天前我遇到了类似的问题.在我的情况下,问题是我用 3rd 方库(准确地说是 jQuery)改变了一些东西,在这种情况下,即使调用函数和设置变量有效,Angular 并不总是认识到有变化,因此它永远不会消化.<块引用>

$apply() 用于从 angular 框架外部执行 angular 表达式.(例如来自浏览器 DOM 事件、setTimeout、XHR 或第三方库).

尝试在更改位置后立即使用 $scope.$apply() 并调用 replace() 让 Angular 知道事情已经改变.>

I'm having an issue with changing the URL of the page after a form has been submitted.

Here's the flow of my app:

  1. Routes are set, URL is recognized to some form page.
  2. Page loads, controller sets variables, directives are fired.
  3. A special form directive is fired which performs a special form submission using AJAX.
  4. After the AJAX is performed (Angular doesn't take care of the AJAX) then a callback is fired and the directive calls the $scope.onAfterSubmit function which sets the location.

The problem is that after setting the location the nothing happens. I've tried setting the location param to / as well... Nope. I've also tried not submitting the form. Nothing works.

I've tested to see if the code reaches the onAfterSubmit function (which it does).

My only thought is that somehow the scope of the function is changed (since its called from a directive), but then again how can it call onAfterSubmit if the scope changed?

Here's my code

var Ctrl = function($scope, $location, $http) {
  $http.get('/resources/' + $params.id + '/edit.json').success(function(data) {
    $scope.resource = data;
  });

  $scope.onAfterSubmit = function() {
    $location.path('/').replace();
  };
}
Ctrl.$inject = ['$scope','$location','$http'];

Can someone help me out please?

解决方案

I had a similar problem some days ago. In my case the problem was that I changed things with a 3rd party library (jQuery to be precise) and in this case even though calling functions and setting variable works Angular doesn't always recognize that there are changes thus it never digests.

$apply() is used to execute an expression in angular from outside of the angular framework. (For example from browser DOM events, setTimeout, XHR or third party libraries).

Try to use $scope.$apply() right after you have changed the location and called replace() to let Angular know that things have changed.

这篇关于AngularJS $location 不改变路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆