我有一个使用angularjs bootstrap-ui创建的客户记录表,并使用ng-repeat。
表格中每行的末尾是一个按钮,用于获取有关客户的更多信息。
单击该按钮时,会弹出一个模态表单。
我的问题是我按哪个按钮我得到相同的客户编号
问题是我需要将$ index的值转换为以下代码:
$scope.selected = {
customer: $scope.customers[0]
};
$ index的值需要替换上面的0值
到目前为止我所做的工作可以在plunker点击here
上看到<div ng-controller="ModalDemoCtrl">
<script type="text/ng-template" id="myModalContent.html">
< div class = "modal-header" > < h3 > I am a modal! < /h3>
</div > < div class = "modal-body" > < form class = "form-horizontal"
role = "form" > < div class = "form-group" > < label
for = "customerNumber"
class = "col-lg-2 control-label" > Email Address: < /label>
<div class="col-lg-10">
<input type="text" class="form-control" id="customerNumber"
ng-model="selected.customer.customerNumber"
ng-value="selected.customer.customerNumber">
</div > < /div>
</form > < /div>
<div class="modal-footer">
<button class="btn btn-primary" ng-click="ok()">OK</button > < button class = "btn btn-warning"
ng - click = "cancel()" > Cancel < /button>
</div >
</script>
<div>
<table class="table table-striped">
<thead>
<tr>
<th>Customer number</th>
<th>Customer name</th>
<th>Customer last name</th>
<th>Customer first name</th>
<th>phone</th>
</tr>
</thead>
<tbody ng-repeat="customer in customers">
<tr>
<td>{{ customer.customerNumber }}</td>
<td>{{ customer.customerName }}</td>
<td>{{ customer.contactLastName }}</td>
<td>{{ customer.contactFirstName }}</td>
<td>{{ customer.phone }}</td>
<td>
<button class="btn btn-default" ng-click="open()">
Customer details
</button>
</td>
</tr>
</tbody>
</table>
</div>
</div>
'use strict';
angular.module('myApp', ['ui.bootstrap'])
.controller('ModalDemoCtrl', function ($scope, $modal, $log) {
$scope.customers = [
{
"customerNumber": 103,
"customerName": "Atelier graphique",
"contactLastName": "Schmitt",
"contactFirstName": "Carine ",
"phone": "40.32.2555"
},
{
"customerNumber": 112,
"customerName": "Signal Gift Stores",
"contactLastName": "King",
"contactFirstName": "Jean",
"phone": "7025551838"
},
{
"customerNumber": 114,
"customerName": "Australian Collectors, Co",
"contactLastName": "Ferguson",
"contactFirstName": "Peter",
"phone": "03 9520 4555"
}
];
$scope.open = function () {
var modalInstance = $modal.open({
templateUrl: 'myModalContent.html',
controller: ModalInstanceCtrl,
resolve: {
customers: function () {
return $scope.customers
}
}
});
modalInstance.result.then(function (selectedCustomer) {
$scope.selected = selectedCustomer;
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
};
});
// Please note that $modalInstance represents a modal window (instance) dependency.
// It is not the same as the $modal service used above.
var ModalInstanceCtrl = function ($scope, $modalInstance, customers) {
$scope.customers = customers;
$scope.selected = {
customer: $scope.customers[0]
};
$scope.ok = function () {
$modalInstance.close($scope.selected.customer);
};
$scope.cancel = function () {
$modalInstance.dismiss('cancel');
}
}
答案 0 :(得分:10)
ng-repeat指令有一个变量
$index
您可以在点击功能中传递此变量,如下所示
<button class="btn btn-default" ng-click="open($index)">
Customer details
</button>
您需要在方法中接受此索引作为参数,因此只需添加参数
即可$scope.open = function (index) {
.... your method body
}
答案 1 :(得分:5)
首先将客户对象传递给您的函数:
ng-click="ok(customer)"
然后在函数中找到基于该对象的索引:
$scope.ok = function (customer) {
var index = $scope.customers.indexOf(customer);
$scope.selected.customer = $scope.customers[index];
$modalInstance.close($scope.selected.customer);
};
答案 2 :(得分:3)
不要这样做 - 而是与实际的客户对象一起工作。如果我错了,请纠正我,但看起来你有某种客户列表,当你点击它们时会打开一个包含更多细节的模态。尝试这样的事情:
在客户表中:
<tbody ng-repeat="customer in customers">
<tr>
<td>{{ customer.customerNumber }}</td>
<td>{{ customer.customerName }}</td>
<td>{{ customer.contactLastName }}</td>
<td>{{ customer.contactFirstName }}</td>
<td>{{ customer.phone }}</td>
<td>
<button class="btn btn-default" ng-click="open(customer)">
Customer details
</button>
</td>
</tr>
</tbody>
在控制器中:
$scope.open = function(customer){
var modalInstance = $modal.open({
templateUrl: 'myModalContent.html',
controller: ModalInstanceCtrl,
resolve: {
customer: function () {
return customer;
}
}
});
modalInstance.result.then(function (selectedCustomer) {
$scope.selected = selectedCustomer;
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
};
在模态控制器中:
var ModalInstanceCtrl = function ($scope, $modalInstance, customer) {
$scope.customer = customer;
$scope.ok = function () {
$modalInstance.close($scope.customer);
};
$scope.cancel = function () {
$modalInstance.dismiss('cancel');
}
}
最后,在模态视图中:
<script type="text/ng-template" id="myModalContent.html">
< div class = "modal-header" > < h3 > I am a modal! < /h3>
</div > < div class = "modal-body" > < form class = "form-horizontal"
role = "form" > < div class = "form-group" > < label
for = "customerNumber"
class = "col-lg-2 control-label" > Email Address: < /label>
<div class="col-lg-10">
<input type="text" class="form-control" id="customerNumber"
ng-model="customer.customerNumber"
ng-value="customer.customerNumber">
</div > < /div>
</form > < /div>
<div class="modal-footer">
<button class="btn btn-primary" ng-click="ok()">OK</button > < button class = "btn btn-warning"
ng - click = "cancel()" > Cancel < /button>
</div >
</script>
答案 3 :(得分:0)
感谢您对Rob和Fourth的建议。
我结合使用了你的建议
点击here
,您可以在plunker上看到结果更改如下所示
<td>
<button class="btn btn-default"
ng-click="open(customer)"> <!-- Customer object now goes to controller-->
Customer details
</button>
</td>
在控制器内部,客户对象仅在开放功能的范围内。
我必须得到索引,以便我可以将它与客户对象一起使用
resolve: {
customerIndex: function () {
return $scope.customers.indexOf(customer)
},
customers: function () {
return $scope.customers
}
}
客户和customerIndex都在ModalInstanceCtrl
中使用var ModalInstanceCtrl = function ($scope, $modalInstance, customers, customerIndex) {
$scope.customers = customers;
$scope.selected = {
customer: $scope.customers[customerIndex]
};
答案 4 :(得分:0)
如果直接使用$index
,它应该更简单。因此,您不需要使用控制器中的客户对象查找索引。请查看以下内容,也可以在plunker
<td>
<button class="btn btn-default" ng-click="open($index)">
Customer details
</button>
</td>
// code above this controller method wasn't modified
$scope.open = function (index) {
var modalInstance = $modal.open({
templateUrl: 'myModalContent.html',
controller: ModalInstanceCtrl,
resolve: {
index: function() {
return index;
},
customers: function () {
return $scope.customers
}
}
});
modalInstance.result.then(function (selectedCustomer) {
$scope.selected = selectedCustomer;
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
};
});
var ModalInstanceCtrl = function ($scope, $modalInstance, customers, index) {
$scope.customers = customers;
$scope.selected = {
customer: $scope.customers[index]
};
$scope.ok = function () {
$modalInstance.close($scope.selected.customer);
};
$scope.cancel = function () {
$modalInstance.dismiss('cancel');
}
}