简体   繁体   English

AngularJS作用域变量未在jQuery Promise / deferred中更新

[英]AngularJS scope variables not updating in jQuery promise/deferred

I am building a single page application on AngularJS and I have a controller set up with a function that is run on a button click. 我正在AngularJS上构建一个单页应用程序,并且我设置了一个controllercontroller具有单击按钮时运行的功能。 This function runs with a promise. 该功能运行良好。 When the function is resolved I am updating a root variable and changing the $location path. 函数解析后,我将更新根变量并更改$location路径。 But the root variable and $location dont seem to be updating. 但是根变量和$location似乎没有更新。

Please note this all code is exampled from production 请注意,所有代码均来自生产示例

DOM: DOM:

<div ng-controller="ExampleController">
    <button ng-click="button_function('I am a variable')">Run function</button>
</div>

Controller: 控制器:

app.controller('ExampleController', ['$scope', '$location', function($scope, $location) {

    $scope.button_function = function(variable) {
        $scope.$root.show_something = true;

        my_function.something(variable).done(function(data) {
            if (data) {
                $scope.$root.show_something = false;
                $location.path('/go-to-path');
            } else {
                alert('Something went wrong');
            }
        }]);
    };

}]);

This is the my_function code: 这是my_function代码:

var my_function = {
    something: function(variable) {
        var deferred = $.Deferred();

        var window = window.open('http://dynamic.url/', '_blank');

        $(window).on('loadstart', function(e) {
            var url = e.originalEvent.url;

            if (url === 'http://dynamic.url/expected_response') {
                window.close();
                deferred.resolve({
                    key_1: 'data',
                    key_2: 'more data'
                });
            }

        });

        return deferred.promise();
    }
};

All looks good right? 看起来都不错吧? But when the my_function.something(variable) is "done" the $location and $scope.$root.show_something don't seem to update. 但是,当my_function.something(variable)被“完成”时, $location$scope.$root.show_something似乎没有更新。

Am I doing something wrong? 难道我做错了什么?

Thanks 谢谢

You should return deferred.promise instead of deferred.promise() . 您应该返回deferred.promise而不是deferred.promise()

--edit: my bad I didn't see you are not using $q as I misread. --edit:不好意思,我没有看到您因为误读而没有使用$ q。

I have found the fix. 我找到了解决方法。

In my controller after the deferred is "done" I wrapped my variables in $timeout 在我的控制器中,“ deferred ”完成后,我将变量包装在$timeout

app.controller('ExampleController', ['$scope', '$location', '$timeout', function($scope, $location, $timeout) {

    $scope.button_function = function(variable) {
        $scope.$root.show_something = true;

        my_function.something(variable).done(function(data) {
            if (data) {
                $timeout(function() {
                    $scope.$root.show_something = false;
                    $location.path('/go-to-path');
                }, 1);
            } else {
                alert('Something went wrong');
            }
        }]);
    };

}]);

Answer found here 这里找到答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM