AngularJS:如何在控制器之间传递变量?
2022-08-29 23:50:45
我有两个角度控制器:
function Ctrl1($scope) {
$scope.prop1 = "First";
}
function Ctrl2($scope) {
$scope.prop2 = "Second";
$scope.both = Ctrl1.prop1 + $scope.prop2; //This is what I would like to do ideally
}
我不能在里面使用,因为它是未定义的。但是,如果我试图像这样传递它...Ctrl1
Ctrl2
function Ctrl2($scope, Ctrl1) {
$scope.prop2 = "Second";
$scope.both = Ctrl1.prop1 + $scope.prop2; //This is what I would like to do ideally
}
我收到一个错误。有人知道如何做到这一点吗?
行为
Ctrl2.prototype = new Ctrl1();
也会失败。
注意:这些控制器不嵌套在彼此内部。