0

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

3
  • eval("(function(){return " + message + ";})()")? Why not just function(){return " + message + ";})()? Commented Jul 2, 2014 at 8:04
  • thanks for it but this don't solve my problem. Commented Jul 2, 2014 at 8:12
  • @dfsq or even shorter obj = message. There is no need for a self invoking function. Commented Jul 2, 2014 at 8:45

1 Answer 1

2

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();
Sign up to request clarification or add additional context in comments.

2 Comments

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.
thanks doup. I add scope.applay() in my searchBarCtrl and scope.watch() in my searchResultCtrl. It's work for me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.