简体   繁体   English

父子控制器的角度JS错误

[英]Angular JS Error with Parent Child controller

I am getting a weird error with angular controllers. 我在使用角度控制器时遇到了一个奇怪的错误。 The error is reproduced on this JSFiddle A sample of HTML: 该错误在此JSFiddle 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>

JavaScript: JavaScript的:

    function GroupViewerController($scope) {
  $scope.selectedObject = "test";
  $scope.arr = ["a","b"]
}

function OneGroupViewerController($scope) {
  $scope.change = function (a){
       $scope.$parent.selectedObject = a;
  }
}

function oneGroupItemsController($scope) {

}

Errors: 错误:

  1. Why does "test" not appear in the textbox though the parent controller object has been referenced 尽管已引用父控制器对象,但为什么“测试”未出现在文本框中
  2. when the button change is pressed, why does the textbox contains the new value of selectedObject 当按下按钮更改时,为什么文本框包含selectedObject的新值

You have made a small mistake I guess. 我猜你犯了一个小错误。 Use the below code and let me know if it works. 使用以下代码,让我知道它是否有效。 working code here 这里的工作代码

<div ng-app>
   <div ng-controller="GroupViewerController">
      <table class="table table-striped" >
            <tr ng-repeat="a in $parent.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.selectedObject">
      </div>
  </div>
</div>

// JS file // JS文件

function GroupViewerController($scope) {
  $scope.selectedObject = {};    // this you have to change in your code. 
  $scope.selectedObject.test = "test";
  $scope.arr = ["a", "b"]
}

function OneGroupViewerController($scope) {
  $scope.change = function(a) {
    $scope.$parent.selectedObject.test = a;
  }
}

function oneGroupItemsController($scope) {

}

Go here to understand the concept. 这里了解概念。

You have made minor mistakes and also its not a good idea to use ng-controller with ng-repeat. 您犯了一些小错误,并且将ng-controller与ng-repeat一起使用也不是一个好主意。

HTML: 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 ng-controller="oneGroupItemsController">
    <input type="text" ng-model="$parent.updateVar.selectedObject">
  </div>
  </div>

  </div>

JS: JS:

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

}

Working fiddle : https://jsfiddle.net/3L4gg6jv/7/ 工作提琴: https : //jsfiddle.net/3L4gg6jv/7/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM