Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
    
eval("(function(){return " + message + ";})()")? Why not just function(){return " + message + ";})()? –  dfsq Jul 2 '14 at 8:04
    
thanks for it but this don't solve my problem. –  user3790694 Jul 2 '14 at 8:12
    
@dfsq or even shorter obj = message. There is no need for a self invoking function. –  Dieter Goetelen Jul 2 '14 at 8:45

1 Answer 1

up vote 1 down vote accepted

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();
share|improve this answer
    
Yep, use $scope.$apply() to notify angular when something from outside updates and $scope.$watch() to notify code outside of angular. That way you can manage state between angular and external code. –  doup Jul 2 '14 at 8:31
1  
thanks doup. I add scope.applay() in my searchBarCtrl and scope.watch() in my searchResultCtrl. It's work for me –  user3790694 Jul 2 '14 at 9:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.