lim*_*mit 5 javascript angularjs ionic-framework
我看了一些例子,但似乎无法弄清楚这一点。基本上,我在 ionic 应用程序中有一个联系表,允许用户联系列表所有者。
当他们提交表单时,我想将广告 ID 存储在本地存储中,以便他们无法重复提交。
我需要能够存储 json 数组,然后检查结果。如果广告 ID 在会话存储中,则不要显示表单,否则显示它。
我目前正在这样做,它似乎将广告 ID 存储在一个数组中,但是如何循环检查 ID 是否存在?我尝试了 angular forEach,但结果是一个对象。
// Parse any JSON previously stored in allEntries
var existingEntries = JSON.parse(localStorage.getItem("store_owner_ad_contacts"));
if(existingEntries == null) existingEntries = [];
var adId = {
"id":$scope.adId
};
// Save allEntries back to local storage
existingEntries.push(adId);
localStorage.setItem("store_owner_ad_contacts", JSON.stringify(existingEntries));
var values = JSON.parse(localStorage.getItem("store_owner_ad_contacts"));
angular.forEach(values, function(value, key) {
// ^ This is coming as an object how can I get the key value?
if(value == adId){
//form has been submitted before
}else{
// showformVar = true
console.log(key + ': ' + value);
});
Run Code Online (Sandbox Code Playgroud)
我的存储看起来像这样
[{"id":"100033"},{"id":"100035"},{"id":"1000336"}]
Run Code Online (Sandbox Code Playgroud)
我如何获得id值?(例如 1000033)
在你的循环中你有一个对象,所以只需像访问对象一样访问 id 属性:object.yourProperty 或 object[yourProperty]
var values = JSON.parse(localStorage.getItem("store_owner_ad_contacts"));
values.forEach(values, function(item, i) {
// you get object like this {id: "100033"}
// so to access id do like normal object
if (item.id === '100033') {
// do something
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
22058 次 |
| 最近记录: |