2014-12-16 9 views
0

userController에서 testController의 Listing 함수를 호출하려고합니다. 다음 코드에서 함수를 호출 할 곳을 (//) 표시합니다. 당신이 둥지 마크 업이 두 컨트롤러, 당신은 자식 컨트롤러에서 부모 범위 기능에 액세스 할 수있는 경우 내가 에서이 일을하고는angularjs에있는 다른 컨트롤러에서 한 컨트롤러의 기능을 호출하는 방법은 무엇입니까?

function userController($scope, $http) { 

       $scope.SignUp = function() { 

        $http.post('<?php echo site_url('angularjs/addToTable'); ?>', {'uname': $scope.username, 'pswd': $scope.userpassword, 'email': $scope.useremail} 
        ).success(function(data, status, headers, config) { 

         // Here i want to call Listing function of testController ... 
        }); 
       } 
    } 
    function testController($scope, $http) { 
     $scope.Listing = function() { 
        $scope.users = []; 
        $http.get('<?php echo site_url('angularjs/get_list'); ?>').success(function($data) { 

         $scope.users = $data; 
        }); 
       } 
    } 

답변

0

을 AngularJS와. 그렇지 않은 경우이 함수는 컨트롤러가 아닌 서비스, 공급자 또는 공장에 속할 수 있습니다.

<div ng-controller="testController"> 
    <div ng-controller="userController"> 

    </div> 
</div> 
0

컨트롤러에 제공 할 수있는 services을 정의 할 수도 있습니다.

myModule.service('UserManagement', function() { 
    this.list = function() { 
     //.. 
    } 

    this.signUp = function() { 
     //.. 
    } 
    }); 

이 서비스는 다음과 같이 컨트롤러에 액세스 할 수 있습니다 :

testController($scope, $http, UserManagement){ 
// insert code here 
} 
여기에는 서비스 userManagement를 정의 할 수 있습니다
관련 문제