Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 父子控制器的角度JS错误_Javascript_Html_Angularjs - Fatal编程技术网

Javascript 父子控制器的角度JS错误

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

我的角度控制器出现了一个奇怪的错误。该错误会在本页上重现 HTML示例:

    <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) {
    
    }