Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/408.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript $location在第一次单击回调时不导航_Javascript_Angularjs_Angularjs Routing - Fatal编程技术网

Javascript $location在第一次单击回调时不导航

Javascript $location在第一次单击回调时不导航,javascript,angularjs,angularjs-routing,Javascript,Angularjs,Angularjs Routing,我的AngularJS应用程序中发生了一些奇怪的事情。我有一个视图,其中包含绑定到对象数组的锚定标记列表(我正在使用ng repeat渲染列表)。每个锚定标记都有一个绑定到控制器上的方法的ng click。方法如下: $scope.onSiteSelected = function ($event, site) { $event.preventDefault(); $.ajax({ url: '/Site/GetSiteMap/'

我的AngularJS应用程序中发生了一些奇怪的事情。我有一个视图,其中包含绑定到对象数组的锚定标记列表(我正在使用
ng repeat
渲染列表)。每个锚定标记都有一个绑定到控制器上的方法的
ng click
。方法如下:

    $scope.onSiteSelected = function ($event, site) {
        $event.preventDefault();
        $.ajax({
            url: '/Site/GetSiteMap/' + site.Id,
            success: function (response) {
                if (response && response.length > 0) {
                    menuManager.setSiteMap($scope.$root.menu, response[0]);
                    var url = response[0].Children[0].Settings.Url;
                    $location.path(url);
                }
            }
        });
    }   
url
var每次都被初始化为正确的值。但是,当我第一次单击锚定标记时,
$location.path(url)
什么也不做,当我第二次单击它时,它会导航到目标url

更新:

好的,我使用
$http
服务实现了如下功能:

    $scope.onSiteSelected = function ($event, site) {
        $event.preventDefault();
        var address = '/Site/GetSiteMap/' + site.Id;
        $http.get(address)
            .success(function (response) {
                if (response && response.length > 0) {
                    menuManager.setSiteMap($scope.$root.menu, response[0]);
                    var url = response[0].Children[2].Settings.Url;
                    $location.path(url);
                }
            });
    }

如果我使用
$、ajax
$http
,这真的有什么不同吗?我认为两者可以互换使用…?

不,您不能互换使用它们,
$。ajax
jQuery
,而
$http
是angular的http服务

不要急于使用jQuery,因为几乎总是有一种方法可以以角度的方式来完成它

这就是说,如果您在angular世界之外做了一些事情(大部分回调不是从angular发生的),您需要应用更改来强制执行
摘要
循环

$.doSth(function callback() {
    $scope.$apply(function () {
        // your actual code
    });
}); 

请阅读中投票最多的答案。这将引导您避免在有其他选择的地方使用jQuery。

啊,我明白了。问题是,这是我的第一个AngularJS项目,我的队友已经在一个使用AngularJS的项目上工作过。她在使用
$http
服务进行跨源请求时遇到了各种各样的麻烦。最终,她不得不在AngularJS上下文之外执行所有AJAX请求。您知道
$http
是否内置了对CORS请求的支持吗?