AngularJS -表中的指令一行接一行
AngularJS - Directive row by row in a table
我有一个通过PHP生成的表格,同时由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:"€"}}'>
</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-model的输入都会得到相同的值。
有没有一种方法在Angular中,也许有id,允许我逐行编译,而不改变其他人?
不好意思,英语不好,谢谢!
PS:我不能使用ng-repeat
您可以为calcolo
使用数组,并在PHP中增加每一行的索引:ng-model="calcolo[0].abbatt"
, ng-model="calcolo[1].abbatt"
等
然后在你的控制器中你可以循环遍历calcolo数组。
正如@yBrodsky在评论中提到的,在每个<tr>
上使用控制器,因此您可以为每个<tr>
具有不同的作用域。
<script data-require="angular.js.1.3@*" 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:"€"}}'>
</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>
相关文章:
- 在指令控制器中使用$attrs时出现问题
- 参数变量出现ngTable指令问题
- 在AngularJS应用程序中使用封装指令和路由的推荐方式是什么
- 使用每次都不同的transclude重复指令
- 打开一个模态并将其链接到AngularJS中的指令
- 从html创建一个指令,该指令按类名应用函数
- 将JSON对象传递给angular指令
- 如何使用ngrepeat和双向绑定获得指令的隔离范围
- 拆分文本以每隔n个字符添加一行新行,并注意空格
- JavaScript指令不能像我想象的那样工作
- 动态插入的表:JQuery未检测到最后一行
- AngularJs指令,该指令创建内部有数据对象的新指令
- AngularJS指令只识别双向绑定类型
- 从控制器继承了隔离的作用域以生成可重用的指令
- AngularJS指令出错-无法读取属性'编译'的未定义
- 在终端中运行 JavaScript 时(使用 rhino),如何使用 print() 函数在一行中打印
- 如何在Angular UI网格中选择下一行
- Angular指令在alertify setContent内容中不起作用
- AngularJS -表中的指令一行接一行
- 如何在一行而不是两行中获得多个使用angular指令的元素