简体   繁体   English

如何在刷新页面时将先前的ng-click初始化值保留为ng-init值

[英]how to keep previous ng-click initialized value as ng-init value upon refreshing the page

I have ng-repeat in which for each image the count of like and dislike buttons are changed. 我有ng-repeat ,对于每个图像, 喜欢不喜欢的按钮的数量都会改变。

However, upon refreshing the page it is giving the ng-init value instead of the previous incremented count of the like and dislike value. 但是,刷新页面时,它会提供ng-init值,而不是之前增加的likedislike值的计数。

How do I keep the incremented value even after refreshing the page? 即使刷新页面后,如何保持增量值?

<button class="btn btn-default" id="{{$index}}" ng-click="count1=count1+1" ng-init="count1=15">Upvote <span class="badge">{{count1}}</span></button>

<button class="btn btn-default" id="{{$index}}" ng-click="count=count+1" ng-init="count=15">Downvote<span class="badge">{{count}}</span></button>

If you hard code the value in the ng-init it will always be the same. 如果您对ng-init的值进行硬编码,则它将始终相同。 You could keep track of the like and dislike count by saving it's value in the HTML 5 localstorage or use an ajax request to save and retreive the dislike data from a server with a database. 您可以通过将喜欢和不喜欢的计数保存在HTML 5 localstorage中的值来跟踪喜欢或不喜欢的计数,或者使用ajax请求从具有数据库的服务器中保存和检索不喜欢的数据。

ng-init will always be evoked on a page refresh. ng-init将始终在页面刷新时触发。 You will need a persistent back end / database of some kind, or as Nicolas stated, HTML 5 localstorage, to save your count values to outside the DOM. 您将需要某种持久的后端/数据库,或者像Nicolas所说的HTML 5 localstorage,以将count数值保存到DOM之外。

You could also use something like json-server . 您也可以使用json-server之类的东西。

Here is an article on setting it up and playing with it. 这是有关设置和使用它的文章。

This article shows you how to connect it with Angular 4. Not the version of Angular you are using, but reading through it may help you conceptualize getting your count data from Angular to a persistent place. 本文向您展示如何将其与Angular 4连接。不是正在使用的Angular版本,但通读它可能有助于您概念化将count数据从Angular获取到持久位置。

您可以将递增的值保存在HTML 5 Storage,数据库和cookie中,因为每当页面重新加载时,它将重新初始化该值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM