使用HTTP时角度双向数据绑定不起作用

时间:2015-11-19 09:33:40

标签: angularjs mvvm angularjs-scope xmlhttprequest

我在 ui.router 的角度中使用了 ControllerAs ,当我调用API并通过vm方法为模板设置范围变量时,我在PHP中有一个API它相应地工作,当我想删除一些记录集 并再次更新 vm.servers 变量,然后模板不会根据新更新的对象进行更改。

function serverController( server, $state, $rootScope, $scope)
{
    var vm  = this;

    vm.delete = function(server_id) {
        vm.loader = false;
        server.delete('server/' + server_id)
            .then(
            function(response){
                if(response.status === 200 && !response.data.status) {
                    alert(response.data.message);

                } else if(response.status === 200 && response.data.status){
                    server.setRootScope().then(
                        function(){
                            vm.servers = $rootScope.servers;
                            $state.go($state.current, {}, {reload: true});
                        }
                    );

                }

            }, function(response) {
                if(response.status === 401) {
                    $state.go('login');
                }
            }
        );
    };


    if($rootScope.servers == undefined) {
        server.get('server')
            .then(
            function (response) {
                if (response.status === 200) {
                    vm.servers = response.data;
                    $rootScope.servers = {};
                    angular.forEach(response.data, function (val) {
                        if('running' === val.status) {
                            val['serverState'] = true;
                        } else {
                            val['serverState'] = false;
                        }
                        $rootScope.servers[val.id] = val;
                    });
                }
            },
            function (response) {
                if (response.status === 401) {
                    $state.go('login');
                }
            });
    } else {
        vm.servers = $rootScope.servers;
    }
}

模板文件。

<table class="table movietable" width="70%" border="1">
    <tr ng-repeat="server in serverModel.servers">
        <td width="85%">
            <table>
                <tr>
                    <td><b>Server Label: </b> {{server.label}}</td>
                </tr>
                <tr>
                    <td>Status: {{server.status}}</td>
                </tr>
                <tr>
                    <td>Created At: {{server.created_at}}</td>
                </tr>
            </table>
        </td>
    </tr>
</table>

1 个答案:

答案 0 :(得分:1)

我看你的代码。我发现了一个问题,

  • 您没有使用更新 vm.servers 响应数据。正如 $ rootScope.servers 中的值可能 年纪大了因此,对于每次删除函数调用,您都必须使用新数据更新 $ rootScope.servers vm.servers
  

我已经从您的代码中创建了一个小型演示,希望它能为您提供帮助   找出问题所在。在本演示中,我首先在表格中加载数据   在此之后,在按钮上单击按ID删除记录并更新    vm.servers

我的控制器

.controller('Controller',['$rootScope', function($rootScope) {
    var vm =this;
    vm.customer = {
      name: 'Naomi',
      address: '1600 Amphitheatre'
    };

    vm.delete = function(server_id) { 
    vm.loader = false;

    // added some value to $rootScope.servers or you can update it with response data. This is where you will need to update your logic.
    $rootScope.servers = [
                            {   id: 1,   
                                name: 'Naomi1',   
                                address: '1600 Amphitheatre1' 
                            },
                            {   
                                id: 2,   
                                name: 'Naomi2',   
                                address: '1600 Amphitheatre2' 
                            }
                          ];  

    angular.forEach($rootScope.servers, function(value,key) {   
        if(value.id == server_id) { 
          $rootScope.servers.splice(key,1); 
        }
    });    

    console.log($rootScope.servers); 

    //Here I have assign new $rootScope.servers.
    vm.servers = $rootScope.servers;

    };

    var val = {};
    vm.init = function() {
            vm.servers = [{
                          id: 1,
                          name: 'Naomi1',
                          address: '1600 Amphitheatre1'
                        },{
                          id: 2,
                          name: 'Naomi2',
                          address: '1600 Amphitheatre2'
                        }];
            $rootScope.servers = {};
            val['serverState'] = true;
            $rootScope.servers[val.id] = val;
        }

    vm.init();

  }])

<强> 的index.html

<div ng-controller="Controller as vm">
  <table class="table movietable" width="70%" border="1">
    <tr>
        <td width="85%">
            <table>
                <tr ng-repeat="server in vm.servers">
                    <td><b>Server Label: </b> {{server.name}}</td>
                    <td>Status: {{server.address}}</td>
                    <td><button ng-click="vm.delete(server.id);">Delete</button></td>
                </tr>
            </table>
        </td>
    </tr>
</table>
</div>

希望这对您有帮助!

干杯, 麦