1

I am getting a weird error with angular controllers. The error is reproduced on this JSFiddle A sample of 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:

    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
5
  • Can you provide jsFiddle ? Commented Sep 25, 2016 at 20:40
  • this fiddle best describes the problem Commented Sep 25, 2016 at 21:11
  • I have edited the question based on the JSFiddle Commented Sep 25, 2016 at 21:16
  • JSFiddle provided has different code than yours. Commented Sep 25, 2016 at 21:20
  • Just re-updated it, sorry Commented Sep 25, 2016 at 21:21

2 Answers 2

1

You have made minor mistakes and also its not a good idea to use ng-controller with ng-repeat.

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:

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/

Sign up to request clarification or add additional context in comments.

4 Comments

I want to bind "OneGroupViewerController" to the row not the entire table
Here you go Noor, have updated the code. let me know if that works for you.
thanks, The answer works, but I do not understand why we have to define selectedObject in updateVar and why can't we do something like $scope.selectedObject = "test"; directly ?
1

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

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.

2 Comments

I want to bind "OneGroupViewerController" to the row not the entire table
Accept the answer if it helped you.:)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.