2016-05-06 86 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/