Javascript 父子控制器的角度JS错误
我的角度控制器出现了一个奇怪的错误。该错误会在本页上重现 HTML示例:Javascript 父子控制器的角度JS错误,javascript,html,angularjs,Javascript,Html,Angularjs,我的角度控制器出现了一个奇怪的错误。该错误会在本页上重现 HTML示例: <div ng-app> <div ng-controller="GroupViewerController"> <table class="table table-striped"> <tr ng-repeat="a in arr" ng-controller="OneGroupViewerController"&g
<div ng-app>
<div ng-controller="GroupViewerController">
<table class="table table-striped">
<tr ng-repeat="a in arr" ng-controller="OneGroupViewerController">
<td >{{a}} <button ng-click="change(a)">change</button></td>
</tr>
</table>
</div>
<div ng-controller="oneGroupItemsController">
<input type="text" ng-model="$parent.selectedObject">
</div>
</div>
function GroupViewerController($scope) {
$scope.selectedObject = "test";
$scope.arr = ["a","b"]
}
function OneGroupViewerController($scope) {
$scope.change = function (a){
$scope.$parent.selectedObject = a;
}
}
function oneGroupItemsController($scope) {
}
错误:
<div ng-app>
<div ng-controller="GroupViewerController">
<table class="table table-striped">
<tr ng-repeat="a in arr" ng-controller="OneGroupViewerController">
<td >{{a}} <button ng-click="change(a)">change</button></td>
</tr>
</table>
</div>
<div ng-controller="oneGroupItemsController">
<input type="text" ng-model="$parent.selectedObject">
</div>
</div>
function GroupViewerController($scope) {
$scope.selectedObject = "test";
$scope.arr = ["a","b"]
}
function OneGroupViewerController($scope) {
$scope.change = function (a){
$scope.$parent.selectedObject = a;
}
}
function oneGroupItemsController($scope) {
}
selectedObject
我想你犯了一个小错误。使用下面的代码,让我知道它是否有效。工作代码
去理解这个概念。您犯了一些小错误,而且将ng控制器与ng repeat一起使用也不是一个好主意 HTML:
工作提琴:你能提供吗?这个提琴最好地描述了这个问题。我根据提供的JSIDdle编辑了这个问题。JSIDdle的代码与你的不同。只需重新更新它,对不起,我想将“OneGroupViewerController”绑定到行而不是整个表。如果有帮助,请接受答案。:)我想绑定“OneGroupViewerController”到该行,而不是整个表。您现在可以开始了,或者,已经更新了代码。让我知道这是否对您有效。谢谢,答案是有效的,但我不明白为什么我们必须在updateVar中定义selectedObject,为什么我们不能直接执行类似$scope.selectedObject=“test”的操作?参考:
<div ng-app>
<div ng-controller="GroupViewerController">
<table class="table table-striped" >
<tr ng-repeat="a in arr" ng-controller="OneGroupViewerController">
<td >{{a}} <button ng-click="change(a)">change</button></td>
</tr>
</table>
<div ng-controller="oneGroupItemsController">
<input type="text" ng-model="$parent.updateVar.selectedObject">
</div>
</div>
</div>
function GroupViewerController($scope) {
$scope.updateVar = {};
$scope.updateVar.selectedObject = "test";
$scope.arr = ["a","b"]
}
function OneGroupViewerController($scope) {
$scope.change = function (a){
$scope.$parent.updateVar.selectedObject = a;
}
}
function oneGroupItemsController($scope) {
}