0
我想將Json文件放入Cookie中並顯示在html中但不起作用。這裏是我的代碼位置0的JSON中的意外令牌v angularjs
服務
.factory('dataServices',dataServices);
function dataServices(){
var cart =[];
var services = {
cart : cart,
addCart : addCart
}
return services
function addCart(tensp,gia,img,sl){
cart.push({tensp:tensp,gia:gia,img:img,sl:sl})
}
}
而且在JS
function shopController(dataServices,$cookies){
var vm=this;
vm.cart = dataServices.cart;
vm.cookies = $cookies.getObject('cart'); // when I add it have error
vm.add = function(tensp,gia,img){
dataServices.addCart(tensp,gia,img,1);
$cookies.putObject('cart',vm.cart);
console.log($cookies.getObject('cart'))
}
}
的Html
<button class="btn btn-primary"
ng-click="vm.add(data.tensp,data.gia,data.img)">
Add to Cart</button>
這裏是執行console.log
[{"tensp":"CPU INTEL CORE I5","gia":"400","img":"item14.jpg","sl":1}]
數據顯示
但當我嘗試寫:vm.cookies = $cookies.getObject('cart');
它有錯誤
意外標記V IN JSON在位置0控制檯
什麼導致你得到關於執行console.log($ cookies.get(「購物車」))線? –
@ImmanuelKirubaharanS它是我編輯的getObject,並且正在收集有問題的數據 –
您可以打開開發人員工具 - >應用程序 - > cookies-> yourdomain.com並截圖它包含的內容嗎? – Leguest