Scoping issue - page not being updated from http post in sibling controller

前端 未结 2 939
难免孤独
难免孤独 2021-01-28 03:00

I am using ng-click to call a function that request new content for the scope. Im doing that by calling a post request that is handeled by php. When I run the script I can see t

2条回答
  •  执念已碎
    2021-01-28 04:06

    The problem is that two controllers are on separate scopes. Put the common data on the scope of a parent controller:

    
      
      
    ̶<̶d̶i̶v̶ ̶i̶d̶=̶"̶o̶b̶j̶e̶c̶t̶_̶c̶o̶n̶t̶a̶i̶n̶e̶r̶"̶ ̶n̶g̶-̶r̶e̶p̶e̶a̶t̶=̶"̶o̶b̶j̶e̶c̶t̶ ̶i̶n̶ ̶o̶b̶j̶e̶c̶t̶s̶ ̶t̶r̶a̶c̶k̶ ̶b̶y̶ ̶o̶b̶j̶e̶c̶t̶.̶i̶d̶"̶>̶
    {{object.name}}
    angular_app.controller('objectNavCtrl', ['$scope','$http', function ($scope,$http) {
    
        $scope.update = function(){
            console.log('clicked');
            $http({
                method: 'post',
                url: 'ajax-processor.php',
                data:  {'ajaxKey':'Mykey'}
            }).then(function successCallback(response) {
                ̶$̶s̶c̶o̶p̶e̶.̶o̶b̶j̶e̶c̶t̶s̶ ̶=̶ ̶r̶e̶s̶p̶o̶n̶s̶e̶.̶d̶a̶t̶a̶;̶
                $scope.common.objects = response.data;
                console.log(response.data);
            });
        }
     }]);
    

    For more information, see

    • AngularJS Developer Guide - Scope Hierarchies
    • AngularJS Wiki - Understanding Scopes.

提交回复
热议问题