我在角度js集合上执行.push()方法时遇到了一个奇怪的问题。在控制台中我可以看到添加了对象,但我实际上看不到它已添加到列表中。
$scope.discountCodes.push({
discountCodeId: 0,
name: $scope.discountModel.name,
code: $scope.discountModel.code,
codeValue: $scope.discountModel.codeValue,
valueType: $scope.discountModel.valueType,
startDate: $scope.discountModel.startDate,
endDate: $scope.discountModel.endDate,
isActive: "True"
});
我有一个简单的转发器和一个模板
<div ng-repeat="discount in discountCodes" ng-include="getTemplate(discount)">
</div>
<script type="text/ng-template" id="display">
<div class="row">
<div class="col-md-3">
<span>Name:<br />{{discount.Name}}</span>
</div>
<div class="col-md-2">
<span>Code:<br />{{discount.Code}}</span>
</div>
<div class="col-md-2">
<span>Value:<br />{{discount.CodeValue}}</span>
</div>
<div class="col-md-2">
<span>Active:<br /></span>
<i class="icon-circle green-fill"
ng-show="discount.IsActive">
</i>
<i class="icon-circle red-fill"
ng-show="!discount.IsActive">
</i>
</div>
<div class="col-md-2"><br />
<a href="#" ng-click="editDiscount(discount)" id="lnkEditRow" name="lnkEditRow" class="gray-fill"><i class="icon-edit icon-2x"></i></a>
</div>
</div>
</script>
这是方法: $ scope.discountModel.formSubmit = function(item,event){
$scope.alertMessageContainerVisible = false;
if ($scope.frmDiscountForm.$valid) {
var dataObject = {
discountCodeId: 0,
name: $scope.discountModel.name,
code: $scope.discountModel.code,
codeValue: $scope.discountModel.codeValue,
valueType: $scope.discountModel.valueType,
startDate: $scope.discountModel.startDate,
endDate: $scope.discountModel.endDate,
isActive: "True"
};
action = "NEW";
$scope.discountCodes.push(dataObject)
});
}
}
任何想法都有帮助,我是Angular JS的新手,所以对我来说很容易:)
我在下面创建了一个非常简单的版本: http://plnkr.co/edit/qJDU7uiFleWIOjR5LYFh
答案 0 :(得分:2)
看起来您可能正在更新Angular上下文之外的集合。如果是这样,您将需要使用$ scope。$ apply()来查看您的更改。
答案 1 :(得分:0)
原来是一个愚蠢的错误!我的控制器在身体和div中定义,所以它在两个地方。现在一切都很好!
谢谢大家,这是一次很好的学习经历