user3790694
user3790694

Reputation: 353

Angular.js update scope after variable has changed

I have a search-bar in my web page, which is associated with a controller that receives a JSON object as a response from the server.

I then save this response in a global variable named assetResult.

It works fine the first time but when I do a new search the $scope of searchResultController is not updated

CONTROLLER SEARCH BAR

mwm3.controller('searchBarCtrl', function($scope, $location, $timeout, AssetService) {
        $scope.radioValue = 'id';
        AssetService.connect();
        AssetService.subscribe(function(message) {
            var obj;
            try {
                //$scope.ocw.push(message);
                obj = eval("(function(){return " + message + ";})()");
                AssetResult = obj;
                console.log(message);

                $location.url('/searchResult');
            } catch (e) {
                obj = eval("(function(){return " + message + ";})()");
                alert(obj.Error);
            }
            //$route.reload();

        });

        $scope.send = function() {
            AssetService.send($scope.radioValue + '=' + $scope.searchKey);

        };

    });

CONTROLLER SEARCH RESULT

mwm3.controller('searchResultCtrl', function($scope, $location, AssetDetailService) {
    $scope.$apply(function() {
        $scope.asm = AssetResult;
    });

    if (!AssetResult) {
        $location.url('/login');
    }
});

I use $scope.apply in my searchResultController but the associated view is not refreshed anyway.

Where is my problem?

Thanks in advance

Upvotes: 0

Views: 567

Answers (1)

Nikolas
Nikolas

Reputation: 1176

It looks to me, as if the message from the AssetService does not start a new angular digest. Try in your searchBarCtrl:

AssetResult = obj;
$scope.$apply();

Upvotes: 2

Related Questions