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

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

问题描述

我有一个表单提交后改变页面的URL的问题。

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. 路线设置,URL被识别为某种形式的页面。

  2. 页面加载时,控制器设置变量,指令被触发。

  3. 一个特殊形式的指令被触发,它利用AJAX一种特殊的形式提交。

  4. 后AJAX执行(角不照顾AJAX的),那么回调被触发,指令调用 $ scope.onAfterSubmit 函数,设置位置。

  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.

我测试过,看看是否code到达 onAfterSubmit 函数(它)。

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

我唯一想到的就是功能的莫名其妙的范围被改变(因为从指令其所谓的),但随后又怎么能叫 onAfterSubmit 如果范围变更?

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?

下面是我的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?

推荐答案

我前几天也有类似的问题。在我的情况的问题是,我改变了一些事情与第三方库(jQuery的是precise),在这种情况下,即使通话功能和设置变量角度的作品并不总是意识到有变化因而它永远不会摘要

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.

$申请()用于从角框架的外侧角来执行一个前pression。 (例如,从浏览器的DOM事件,setTimeout的,XHR或第三方库)。

$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 $位置不改变路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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