0

I have a form submit by regular javascript outsite angular. The form is successfuly submit and update the database. I have a $http inside angularjs to query the data.

My question is once submit the form, how can I refresh data inside angularjs?

sample javascript submit:

$.getJSON('/Api/Cbs/setPost', inputPost, function(rsp){
  if(rsp.status > 0){alert('submited');
}

here is my code in angularjs

$http.get('echo U('Api/Cbs/getPostList')')
.then(function(rsp){
  console.log('angular-getPost', rsp);
  $scope.postList = rsp.data;
});

the $http.get() fire only once on page load, not keep watching the API data update.

Weijing Jay Lin
  • 2,991
  • 6
  • 33
  • 53
  • do access the scope by querying DOM.. and then call `function` which refreshes `postList` data.. you can refer here [how to access scope of dom by querying it](http://stackoverflow.com/a/13744085/2435473) – Pankaj Parkar Mar 20 '16 at 19:44
  • Show the full context for the submit code. What do you do with the response? Seems strange having another submit handler outside of angular – charlietfl Mar 20 '16 at 19:46

1 Answers1

0

You can get scope by DOM search:

angular.element(controllerId).scope().$apply("getHttp();");

Where controllerId - id attribute of you controller tag, e.g.:

<div ng-controller="myController" id="controllerId">

And getHttp() method is declared on scope:

  $scope.getHttp = function () {
    $http.get('echo U('Api/Cbs/getPostList')')
    .then(function(rsp){
      console.log('angular-getPost', rsp);
      $scope.postList = rsp.data;
    });
  };
Slava N.
  • 596
  • 4
  • 6