javascript - How to get two-way data binding with a Service in AngularJS? -


i query json file , result make table, file updated frequently, need see if file has suffered changes show them in real time application.

i have tried many things angularjs every time has result in fails.

this code:

app.js

(function(){    var myapp = angular.module("appmonitor",[]);      myapp.factory('myservice', function($http,$timeout) {       var promise;       var myservice = {          get: function() {              if ( !promise ) {                  // $http returns promise,                  // has function, returns promise                  promise = $http.get('./json/verifyempty.json').then(function (response) {                  // function here opportunity modify response                  console.log(response.data);                  // return value gets picked in controller.                  return response.data;                  });               }               // return promise controller               return promise;            }         };       return myservice;     });       myapp.controller('monitorctrl', function(myservice,$scope) {     // call async method , stuff returned inside our own function         myservice.get().then(function(d) {             $scope.response = d;         });     });  })(); 

verifyempty.json

[   { "name":"luis", "lastname":"perez", "age":27, "gender":"m", "haircolor":"yellow", "eyecolor":"brown", "student":false },   { "name":"monse", "lastname":"chuang", "age":28, "gender":"f", "haircolor":"black", "eyecolor":"black", "student":true },   { "name":"sarah", "lastname":"lee", "age":29, "gender":"f", "haircolor":"yellow", "eyecolor":"green", "student":true },   { "name":"luisa", "lastname":"ortega", "age":28, "gender":"f", "haircolor":"black", "eyecolor":"blue", "student":false },   { "name":"lorenza", "lastname":"garcia", "age":27, "gender":"f", "haircolor":"brown", "eyecolor":"brown", "student":true } ] 

monitor.php

 <body ng-app="appmonitor">     <div class="" ng-controller="monitorctrl">     <table>        <tr>           <th><strong>name</strong></th>           <th><strong>lastname</strong></th>           <th><strong>age</strong></th>           <th><strong>gender</strong></th>           <th><strong>haircolor</strong></th>           <th><strong>eyecolor</strong></th>           <th><strong>student?</strong></th>        </tr>         <tr ng-repeat="r in response" ng-cloak>           <td>{{r.name}}</td>           <td>{{r.lastname}}</td>           <td>{{r.age}}</td>           <td>{{r.gender}}</td>           <td>{{r.haircolor}}</td>           <td>{{r.eyecolor}}</td>           <td ng-show="r.student">yes</td>         </tr>       </table>        <!-- {{response}} -->     </div>     <script type="text/javascript" src="./js/148libraries/angular.min.js"></script>     <script type="text/javascript" src="./js/app.js"></script>  </body> 

you can't want, can achieve similar.

with code have posted, retrieving json contents one time. so, imagine in complete code, executing $http.get() , pooling json files after x seconds using settimeout() or similar. easiest way, not efficient.

since json file hosted in machine, angular code, runs in client's browser can't know when file has changed. must warned else.

in case, first thing need implement @ server-side routine triggered when json file changes. task, seems using php, can take @ inotify , answer.

the second part implement streaming approach in angular instead of pooling mechanism. since don't know php tools well, can't find 1 right suggest you. if use node, suggest take @ pusher , in tutorial.

with approach, set node server listening changes in json file , angular service listening node. whenever file changed, trigger node , angular update table view.

note: one-way data bind. two-way data bind $watch variable changes made users @ browser , when $watch receives event, make $http call update json file.


Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -