2016-05-06 9 views
0

テーブルに複数の行があり、それぞれが最後のセルに選択メニューがあります。初期値は1つのコントローラによって設定され、選択オプションは第2のコントローラによって設定されます。また、第2のコントローラは、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を実装にバインドする方法として使用するためのものであり、データを取得するための機能を提供するものではありません。

あなたのコードをリファクタリングしてページ/テーブル用のコントローラを1つ用意し、このエージェント/リードコードをすべて別のサービスに入れて、コントローラが必要なときに消費するようにします。

は、より多くの洞察力のために、このブログ記事を参照してください。 http://kirkbushell.me/when-to-use-directives-controllers-or-services-in-angular/

関連する問題