在child中调用的父控制器方法

时间:2015-02-16 17:33:48

标签: angularjs

我想从“继承”控制器获取$ scope变量值。它适用于非范围var,但范围var print undefined。

appControllers.controller('parentCtrl', ['$scope', '$rootScope', '$http', '$location', '$log',
    function ($scope, $rootScope, $http, $location, $log) {

        $scope.someVariable = "Scope hello world.";

        $scope.getLocalVariable = function (){
            return "hello world";
        }

        $scope.getScopeVariable = function (){
            return $scope.someVariable;
        }

    }]);


appControllers.controller('childCtrl', ['$scope', '$rootScope', '$http', '$location', '$log',
    function ($scope, $rootScope, $http, $location, $log) {

        $log.debug($scope.getLocalVariable());  // this WORKS

        $log.debug($scope.getScopeVariable());  // this DOES NOT WORKS - prints undefined

    }]);

.html文件的一部分

<div ng-controller="parentCtrl">
<div ng-controller="childCtrl">
</div>
</div>
你知道吗?我也试过

var app = angular.module('angularjs-starter', []);

app.controller('ParentCtrl ', function($scope) {
  // I'm the sibling, but want to act as parent
});

app.controller('ChildCtrl', function($scope, $controller) {
  var ctrl = $controller('ParentCtrl', {$scope: $scope}); 
  ctrl.methodCall() // DOES NOT WORK

});

但它没有帮助。感谢

0 个答案:

没有答案