2016-11-15 21 views
0

指令逐行我通過PHP有一個表generetad同時和AngularJS處理:AngularJS - 在一個表中

<table> 
    <tr> 
    <th>Col 1</th> 
    <th>Col 2</th> 
    <th>Col 3</th> 
    </tr> 


    <?php 
     while(...){ 
    ?> 
     <tr> 
      <td> 
      <input type="text" ng-model="calcolo.ore"> 
      </td> 
      <td> 
      <input type="text" ng-model="calcolo.ricavo"> 
      </td> 
      <td> 
      <input type="text" ng-model="calcolo.abbatt"> 
      </td> 
      <td> 
      <input type='text' ng-show="calcolo.abbatt" value='{{netti() | currency:"&euro;"}}'> 
      </td> 

     </tr> 

    <?php } ?> 

angular.module("myApp", ['ng-currency']) 
    .controller("userController", 
     function($scope) { 

      $scope.fattlord = function() { 
       return ($scope.calcolo.ore * $scope.calcolo.ricavo) 
      }; 


      $scope.netti = function() { 
       return ($scope.calcolo.ricavo-(($scope.calcolo.abbatt * $scope.calcolo.ricavo)/100)) 
      }; 


     }); 

當然,當我寫到一個文本輸入,所有具有相同ng模型的輸入都會得到相同的值。

有沒有一種方法在Angular中,也許帶有ID,它允許我逐行地編譯,而不改變其他的?

對不起,英語不好,謝謝!

PS:我不能用ng-repeat。

+0

您應該創建一個指令([Documentation](https://docs.angularjs.org/guide/directive)) – gianlucatursi

+0

或者爲每個定義一個控制器。那麼每個tr將有不同的作用域 – yBrodsky

+0

「所有具有相同ng模型的輸入都會得到相同的值」 - >這就是應該發生的情況。在你的代碼中,ng-model都是不同的。你能澄清你想達到什麼樣的行爲嗎? – elfan

回答

0

由於@yBrodsky在評論中提到,請在每個<tr>上使用控制器,因此每個<tr>可以有不同的範圍。

<script data-require="[email protected]*" data-semver="1.5.2" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.2/angular.js"</script> 
<body data-ng-app="myApp"> 
<table> 
    <tr> 
     <th>Col 1</th> 
     <th>Col 2</th> 
     <th>Col 3</th> 
    </tr> 
    <?php 
     $i = 0; 
     while($i++ < 5){ 
    ?> 
    <tr data-ng-controller="userController"> 
     <td> 
      <input type="text" ng-model="calcolo.ore"> 
     </td> 
     <td> 
      <input type="text" ng-model="calcolo.ricavo"> 
     </td> 
     <td> 
      <input type="text" ng-model="calcolo.abbatt"> 
     </td> 
     <td> 
      <input type='text' ng-show="calcolo.abbatt" value='{{netti() | currency:"&euro;"}}'> 
     </td> 
    </tr> 
    <?php } ?> 
</table> 
<script> 
    angular.module("myApp", []) 
    .controller("userController", 
     function($scope) { 
      $scope.calcolo = {}; //init 

      $scope.fattlord = function() { 
       return ($scope.calcolo.ore * $scope.calcolo.ricavo) 
      }; 
      $scope.netti = function() { 
       return ($scope.calcolo.ricavo-(($scope.calcolo.abbatt * $scope.calcolo.ricavo)/100)) 
      }; 
     } 
     ); 
</script> 
</body> 
0

你可以使用一個數組爲calcolo,並增加在PHP中的每一行的索引:在您的控制器可以循環在calcolo陣列ng-model="calcolo[0].abbatt"ng-model="calcolo[1].abbatt"

然後。