Angularjs 使用ng repeat和单选按钮时获得“undefined”
在引导UI模式上使用Angular的Angularjs 使用ng repeat和单选按钮时获得“undefined”,angularjs,twitter-bootstrap-3,angular-ui-bootstrap,Angularjs,Twitter Bootstrap 3,Angular Ui Bootstrap,在引导UI模式上使用Angular的ng repeat时,我遇到了一个奇怪的行为 我在我的customerService.js工厂中有这个虚拟方法: app.factory('customerService', [ function() { customerFactory.getCustomers = function () { return [{ "name": "Terry Tibbs" }, { "name": "Bob
ng repeat
时,我遇到了一个奇怪的行为
我在我的customerService.js
工厂中有这个虚拟方法:
app.factory('customerService', [ function() {
customerFactory.getCustomers =
function () {
return [{ "name": "Terry Tibbs" },
{ "name": "Bobby Halls" },
{ "name": "Garry Brisket" }]
};
return customerFactory;
}]);
这是customerSelectionModal.html
modal模板:
<div>
<div class="modal-header">
<h3 class="modal-title">Select a customer</h3>
</div>
<div class="modal-body">
<label data-ng-repeat="cust in customers">
<input name="customer" type="radio" value="{{cust}}" ng-model="$parent.selected.item" />{{cust.name}}
</label>
<div ng-show="selected">You have selected: {{ selected }}</div>
<div ng-show="selected">name: {{ selected.item.name }}</div>
<div class="modal-footer">
<button class="btn btn-primary" ng-click="ok()">OK</button>
<button class="btn btn-default" ng-click="cancel()">Cancel</button>
</div>
</div>
</div>
最后,模态窗体的modalInstanceController.js
控制器:
app.controller('modalInstanceController',
function ($scope, $modalInstance, customers) {
$scope.customers = customers;
$scope.selected = {
item: $scope.customers[0]
};
$scope.ok = function () {
alert($scope.selected.item.name);
$modalInstance.close($scope.selected.item);
};
$scope.cancel = function () {
$modalInstance.dismiss('cancel');
};
});
当模式对话框最初显示时,我得到您选择的:{“项目”:{“名称”:“Terry Tibbs”}
正确显示,因为这是默认客户
由modalInstanceController
控制器选择。但是,当我选择一个客户时,您已经选择:{“项目”:“{”name\“:\“Terry Tibbs\”}
,然后单击确定
按钮只会在警报中显示未定义
我不知道为什么
唯一可能的线索是,当一个客户被选中时,名称
属性,它的值被一个\
转义,原因很奇怪,我还没有弄清楚
有人知道这是怎么回事吗
最后,是否可以将单选按钮设置为所选客户,因为它不会对客户进行选择
我正在使用以下工具:
AngularJS v1.3.9
TwitterBootstrap v3.3.1
Angular UI引导v0.12.0无论如何,在单选按钮中,尝试使用
ng value
而不是value
属性。为什么在单选按钮ng模型中使用$parent?你能使用selected.item吗?那是因为ng repeat
指令为每个项目创建了一个子范围,所以我需要使用$parent
使其使用ng repeat
指令的父范围。好的,实际上我不记得了,对不起。无论如何,在单选按钮中,尝试使用ng value
而不是value
属性。我应该已经发现了!您将代码改为使用ng值
是对的,效果非常好!非常感谢。那么我可以写答案吗
app.controller('modalInstanceController',
function ($scope, $modalInstance, customers) {
$scope.customers = customers;
$scope.selected = {
item: $scope.customers[0]
};
$scope.ok = function () {
alert($scope.selected.item.name);
$modalInstance.close($scope.selected.item);
};
$scope.cancel = function () {
$modalInstance.dismiss('cancel');
};
});