【发布时间】:2017-02-22 17:10:43
【问题描述】:
我有两个控制器 ParentController 和 ChildController 我在 ParentController 中有一个变量,我需要将该变量传递给 ChildController,然后我需要将它传递给视图,还有一件事是我不应该使用 $子控制器和父控制器的范围。有可能吗?如果没有,有没有办法在没有 $scope 的情况下使用它。
app.controller('ParentController', function($scope) {
$scope.exampleVariable = "test";
});
app.controller('ChildController', function($scope, $controller) {
var someScopeVariable = this;
$controller('ParentController', {$scope: someScopeVariable });
console.log(someScopeVariable.exampleVariable)
someScopeVariable.exampleVariable = "Updatetest";
});
现在在我的 html 视图中我需要使用 exampleVariable
喜欢这个
<div ng-controller="ChildController as child">
<h1>{{child.exampleVariable}}</h1>
</div>
如何从父控制器获取值到 html 视图。
【问题讨论】:
标签: angularjs