javascript Angular:将项目推送到列表不会更新视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16589743/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Angular: Push item to list doesn't update the view
提问by Martijn
When I push an item to an array, the view won't refresh the list.
当我将项目推送到数组时,视图不会刷新列表。
table:
桌子:
<tbody id="productRows">
<tr data-ng-repeat="product in products | filter: search">
<td>{{ product.Code}}</td>
<td colspan="8">{{ product.Name}}</td>
</tr>
</tbody>
form:
形式:
<form data-ng-submit="submitProduct()">
Code:
<br />
<input type="text" required data-ng-model="product.Code"/>
<br />
<br />
Naam:
<br />
<input type="text" required data-ng-model="product.Name"/>
<br />
<input type="submit" value="Opslaan" />
</form>
submitProduct in controller:
在控制器中提交产品:
$scope.submitProduct = function () {
console.log('before: ' + $scope.products.length);
$scope.products.push({Code: $scope.product.Code, Name: $scope.product.Name});
console.log('after:' + $scope.products.length);
console.log($scope.products);
$scope.showOverlay = false;
};
As you can see, I log the total items in the array and it behaves like I would expect. The only thing that doesn't do what I expect is the content of my table, that doesn't show the new value.
如您所见,我记录了数组中的总项目数,它的行为与我预期的一样。唯一不符合我预期的是我的表的内容,它没有显示新值。
What do I have to do, so the new row is displayed in the table?
我必须做什么才能在表格中显示新行?
采纳答案by Martijn
Thanks for the answer and the comments. The problem was at another place. In my routeProvider
I had declared a controller. I also had a ng-controller
directive in my div. So my controller gets executed twice. When I removed the ng-controller
directive, everything was just working as it should be :)
感谢您的回答和评论。问题出在另一个地方。在我的routeProvider
我已经声明了一个控制器。ng-controller
我的 div 中也有一个指令。所以我的控制器被执行了两次。当我删除ng-controller
指令时,一切都在正常工作:)
回答by Brett Postin
I can't see the rest of your code, but make sure $scope.products
is defined in your controller.
我看不到您的其余代码,但请确保$scope.products
已在您的控制器中定义。
See this example.
请参阅此示例。
The only addition I made to the code you provided was:
我对您提供的代码所做的唯一补充是:
$scope.products = [];
If this doesn't help then please provide more information.
如果这没有帮助,请提供更多信息。