如何在angular.js中自动更新视图上的数据

时间:2014-09-18 08:03:48

标签: javascript angularjs

我在视图网站上显示了手机数据:

<table class="table table-stripes data-tables" ng-controller="GetData as mydata">
            <tr>
                <th align="center"><div align="center">S.#</div></th>
                <th align="center"><div align="center">Name</div></th>
                <th align="center"><div align="center">Number</div></th>
                <th align="center"><div align="center">Edit</div></th>
                <th align="center"><div align="center">Delete</div></th>
            </tr>
            <tr ng-repeat="alldata in mydata.row | filter:search ">
                <td>{{alldata.id}}</td>
                <td>{{alldata.name}}</td>
                <td>{{alldata.numbers}}</td>
                <td><button class="btn btn-primary">Edit</button></td>
                <td><button class="btn btn-danger">Delete</button></td>
            </tr>
        </table>

当用户添加新的时,我想刷新控制器。我怎么能这样做

//getdata controller


myApp.controller("GetData",['$http','$log','$location','$timeout', function($http,$log,$location,$timeout){
var ata = this;
ata.row = [ ] ;
$http({method: 'POST', url: 'process/getdata.php'})
.success(function(data) {
    // this callback will be called asynchronously
    // when the response is available
//  $log.log(data);
ata.row=data;
  }).
  error(function(data, status, headers, config) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
  });
}]);

// add new item controller



myApp.controller("addNumber",['$http','$log','transformRequestAsFormPost','$location', function($http,$log,transformRequestAsFormPost,$location){

    this.add = function(name,number){

    var mydata='name='+name+'&number'+number;
        $http({
            method: 'POST',
            url: 'process/addnumbers.php',
             data:{
                name: name,
                number: number
             }}).
  success(function(data, status, headers, config) {
    // this callback will be called asynchronously
    // when the response is available
    $log.log(data);
   if(data == 'true')
   {

   }
   else
   {

   }

  }).
  error(function(data, status, headers, config) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
  });
    }
}]);
  • 我应该怎么做getdata控制器
  • 用户添加新记录时应该怎么做

1 个答案:

答案 0 :(得分:0)

尝试在添加号码控制器中刷新页面。这可能会有所帮助。

myApp.controller("addNumber",['$http','$log','transformRequestAsFormPost','$location','$route', function($http,$log,transformRequestAsFormPost,$location,$route){

    this.add = function(name,number){

    var mydata='name='+name+'&number'+number;
        $http({
            method: 'POST',
            url: 'process/addnumbers.php',
             data:{
                name: name,
                number: number
             }}).
  success(function(data, status, headers, config) {
    // this callback will be called asynchronously
    // when the response is available
    $log.log(data);
   if(data == 'true')
   {

        $route.reload();
        // get the current path
        $location.path();

        // change the path
        $location.path('/Dashboard');

   }
   else
   {

   }

  }).
  error(function(data, status, headers, config) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
  });
    }
}]);

在控制器中注入$route服务