javascript - How to connect the ng-model of two different controller in angularjs? -


i have 1 table fill json data. after filling table, if 1 wants modify row pop-up window, can modify.

what doing:
made 2 controller, want pass ng-model value 1 controller other controller controller window. please tell me how connect these 2 controller. please see running example, http://plnkr.co/edit/6lrt1b1sycex0lvcjizz?p=preview

index.html

<!doctype html> <html>  <head> <title>todo api client demo</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script> <script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.0.js"></script> <link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet"> <script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>  </head>  <body>   <div class="navbar">         <div class="navbar-inner">             <a class="brand" href="#">todo api client demo</a>         </div>   </div>   <div ng-app="myapp" ng-controller="tasksctrl">           <table class="table table-striped">              <tr><td style="width: 1px;"></td><td><b>task</b></td><td><b>options</b></td></tr>             <tr ng-repeat="task in tasks">              <td>{{task.title}}</td>             <td>{{task.description}}</td>                         <td>  <a class="btn" data-toggle="modal" data-target="#modal" ng-click="edittask(task)">edit</a></td>               </tr>          </table>    </div>   <div id="modal" role="dialog" class="modal hide fade">         <div ng-controller="taskcontroller">             <div class="modal-header">                 task dialog             </div>             <div class="modal-body">                 <label for="txtname"></label>                  <input type="text"  ng-model="task.title" />                 <input type="text"  ng-model="task.description" />             </div>             <div class="modal-footer">                 <button class="btn btn-primary" ng-click="savetask()" data-dismiss="modal">ok</button>             </div>         </div>    </div>    <script>     var app = angular.module('myapp', []);     app.controller('tasksctrl', function($scope, $http) {         $http.get("data.json")         //$http.get("/todo/api/v1.0/tasks")         .success(function(response) {           console.log(response.tasks)           $scope.tasks = response.tasks;         });     });     app.controller('taskcontroller', function($scope, $rootscope){         $scope.edittask=function(task){             $rootscope.task=task;         }     });    </script> </body>  </html> 

first, place ng-app="myapp" , ng-controller="tasksctrl" body element.

e.g.

<body ng-app="myapp" ng-controller="tasksctrl"> 

then, move the

$scope.edittask=function(task) {      $scope.task = task; }; 

to tasksctrl. taskcontroller no longer needed. can remove it.

since, use 1 controller can use $scope instead of $rootscope.

here's plunkr.

hope helps.


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -