2016-05-06 92 views
0

我在表格中有几行,每个在最后一个单元格中都有一个选择菜单。初始值由一个控制器填充,选择选项由第二个控制器填充。第二个控制器也更新ng-change的值。如果我使用ng-selected,我会得到初始值,但不会更改变化值。 (它确实将它记录到控制台)。如果我使用ng-init,它不会给出加载值,但在更改值后会更新。角度选择值不变

app.controller('agents', function($scope, $http){ 

    $scope.getAgents = function(){ 
     $http.get("getagents.php").then(function(response) { 
      $scope.agents = response.data; 
     }); 
    } 

    $scope.getActiveAgents = function(){ 
     $http.get("activeagents.php").then(function(response) { 
      // console.log(response.data); 
      $scope.activeagents = response.data; 
     }); 
    } 

    $scope.updateAgenttoLead = function(agent, id){ 

     console.log('ID:'+ id); 
     console.log('Agent:'+ agent); 
    } 

    $scope.updateForm = {}; 

    $scope.updateAgent = function() { 
     $http.post('updateagent.php', { 
       'id' : $scope.updateForm.id, 
       'username' : $scope.updateForm.username, 
       'password' : $scope.updateForm.password 
      } 
     ).success(function(data) { 
      // console.log(data); 
      $scope.updateForm = {}; 
      $scope.getAgents(); 
      // if (!data.success) { 
      // // if not successful, bind errors to error variables 
      // $scope.errorName = data.errors.name; 
      // $scope.errorSuperhero = data.errors.superheroAlias; 
      // } else { 
      // // if successful, bind success message to message 
      // $scope.message = data.message; 
      // } 
      }); 
    }; 

    $scope.addForm = {}; 

    $scope.addAgent = function() { 

     $http.put('createagent.php', { 
      'username' : $scope.addForm.username, 
      'password' : $scope.addForm.password, 
      'type' : $scope.addForm.type 
      } 
     ).success(function(data) { 
      $scope.addForm = {}; 
      $scope.getAgents(); 
      }); 
    }; 

    $scope.deleteagent = function(newid){ 

     var r =confirm('Are you sure you want to delete '+ newid+'?'); 

     if(r){ 
      $http.post('deleteagent.php', { 
       'newid':newid 
       } 
      ).success(function(data) { 
       $scope.getAgents(); 
       console.log(data); 
       }); 
     } 
    }; 

}); // end controller 

app.controller('leads', function($scope, $http){ 
    $scope.getLeads = function(){ 
     $http.get("getleads.php").then(function(server) { 
      $scope.leads = server.data; 

     }); 
    } 

    $scope.dispositions =[ 
     'Never Called', 
     'Not Available', 
     'Left Message', 
     'Call Later', 
     'Not Interested', 
     'Not Qualified', 
     'Bad Phone', 
     'No Dates', 
     'DNC', 
     'Post Date', 
     'Sold' 
    ]; 

    $scope.updateDisp = function(disp, id){ 

     var r = confirm('Update record '+ id +' to '+disp+'?'); 
      if(r){ 
       $http.post('updatedisp.php', { 
         'id' : id, 
         'disp' : disp 
        } 
       ).success(function(data) { 
        console.log(data); 
        }); 
      }else{ 
       $scope.leads={}; 
       $scope.getLeads(); 
      } 
    } 
}); // end controller 
+0

我强烈建议针对此问题创造一个Plunker。 – Kyle

回答

2

您正在使用控制器作为服务。控制器旨在用作将UI绑定到实现的一种方式,而不是提供用于检索数据的功能。

我会重构您的代码,为您的页面/表格创建一个控制器,然后将所有代理/代码放在单独的服务中,然后控制器在需要时使用它们。

见本博客文章更深入的了解: http://kirkbushell.me/when-to-use-directives-controllers-or-services-in-angular/