Javascript ng repeat仅显示对象数组中的第一个项目
我正在使用一个Javascript ng repeat仅显示对象数组中的第一个项目,javascript,angularjs,json,sqlite,cordova,Javascript,Angularjs,Json,Sqlite,Cordova,我正在使用一个cordova应用程序,它将显示购物车中的物品列表,这些物品来自设备的sqlite数据库。实际上,我正在为这个项目使用cordova-sqlite插件 这是json输出 { "items": [ { "prod_id": 1, "prod_name": "2 in 1 Retractable USB cable 2", "prod_price": 299, "pr
cordova
应用程序,它将显示购物车中的物品列表,这些物品来自设备的sqlite
数据库。实际上,我正在为这个项目使用cordova-sqlite插件
这是json
输出
{
"items": [
{
"prod_id": 1,
"prod_name": "2 in 1 Retractable USB cable 2",
"prod_price": 299,
"prod_img": "img/prod/2_in_1_retractable.png",
"discount": 50,
"quantity": 1
},
{
"prod_id": 2,
"prod_name": "2 in 1 Screen Protector",
"prod_price": 199,
"prod_img": "img/prod/2_in_1_screen_protector",
"discount": 50,
"quantity": 1
},
{
"prod_id": 3,
"prod_name": "3 in 1 USB Charging cable",
"prod_price": 199,
"prod_img": "img/prod/usb_charging_cable.png",
"discount": 50,
"quantity": 1
}
],
"count": 11
}
编辑
这是html
<a class="item item-thumbnail-left item-button-right" href="#" ng-repeat="cart in carts">
<img src="http://{{host}}/{{cart.prod_img}}">
<h2>{{cart.prod_name}}</h2>
<p>Php {{cart.prod_price}}</p>
<button class="button-positive mini-button button-small" style="margin-top: 9px"
ng-click="change_quantity('inc', $index, cart.prod_id, cart.prod_price, cart.discount, cart.order_limit, cart.invntry_qty)">
<i class="icon ion-plus"></i>
</button>
<input type="number" class="mini-input" style="margin-top: 9px" ng-change="alert('quantity_inputted');"
ng-model-options="{debounce:1000}">
<button class="button-positive button-small mini-button" style="margin-top: 9px; margin-right: 8px;"><i class="icon ion-minus"></i></button>
<button class="button-small button-positive remove icon-left ion-trash-b" style="margin-top: 9px"
ng-click="remove_to_cart(cart.prod_id)"> Remove</button>
</a>
从数据库获取json之后。我把它们放到$scope
$scope.carts = cart.items;
问题是ng repeat仅显示第一个具有产品id:1
的项目。json的json
看起来不错,我不知道我的js
和html
代码有什么问题。我从警报(json.stringify(cart))获取了json输出代码>
谢谢您的帮助。您没有在carts对象内部的数组中循环。下面是工作代码
var myapp=angular.module(“app”,[]);
控制器(“显示车”,函数($scope){
$scope.carts={
“项目”:[
{
“产品id”:1,
“产品名称”:“2合1可伸缩USB电缆2”,
“产品价格”:299,
“prod_img”:“img/prod/2_in_1_retractable.png”,
“折扣”:50,
“数量”:1
},
{
“产品id”:2,
“产品名称”:“2合1屏幕保护器”,
“产品价格”:199,
“产品img”:“屏幕保护器中的img/prod/2”,
“折扣”:50,
“数量”:1
},
{
“产品id”:3,
“产品名称”:“3合1 USB充电电缆”,
“产品价格”:199,
“产品img”:“img/prod/usb\u charging\u cable.png”,
“折扣”:50,
“数量”:1
}
],
“计数”:11
};
});
AngularJSHTMLDOM
AngularJS示例应用程序
您没有在carts对象内部的数组中循环。下面是工作代码
var myapp=angular.module(“app”,[]);
控制器(“显示车”,函数($scope){
$scope.carts={
“项目”:[
{
“产品id”:1,
“产品名称”:“2合1可伸缩USB电缆2”,
“产品价格”:299,
“prod_img”:“img/prod/2_in_1_retractable.png”,
“折扣”:50,
“数量”:1
},
{
“产品id”:2,
“产品名称”:“2合1屏幕保护器”,
“产品价格”:199,
“产品img”:“屏幕保护器中的img/prod/2”,
“折扣”:50,
“数量”:1
},
{
“产品id”:3,
“产品名称”:“3合1 USB充电电缆”,
“产品价格”:199,
“产品img”:“img/prod/usb\u charging\u cable.png”,
“折扣”:50,
“数量”:1
}
],
“计数”:11
};
});
AngularJSHTMLDOM
AngularJS示例应用程序
根据原始帖子中提供的JSON
,您的代码运行良好
演示
var myApp=angular.module('myApp',[]);
myApp.controller('MyCtrl',函数($scope){
$scope.carts=[
{
“产品id”:1,
“产品名称”:“2合1可伸缩USB电缆2”,
“产品价格”:299,
“prod_img”:“img/prod/2_in_1_retractable.png”,
“折扣”:50,
“数量”:1
},
{
“产品id”:2,
“产品名称”:“2合1屏幕保护器”,
“产品价格”:199,
“产品img”:“屏幕保护器中的img/prod/2”,
“折扣”:50,
“数量”:1
},
{
“产品id”:3,
“产品名称”:“3合1 USB充电电缆”,
“产品价格”:199,
“产品img”:“img/prod/usb\u charging\u cable.png”,
“折扣”:50,
“数量”:1
}
];
});代码>
根据原始帖子中提供的JSON
,您的代码运行良好
演示
var myApp=angular.module('myApp',[]);
myApp.controller('MyCtrl',函数($scope){
$scope.carts=[
{
“产品id”:1,
“产品名称”:“2合1可伸缩USB电缆2”,
“产品价格”:299,
“prod_img”:“img/prod/2_in_1_retractable.png”,
“折扣”:50,
“数量”:1
},
{
“产品id”:2,
“产品名称”:“2合1屏幕保护器”,
“产品价格”:199,
“产品img”:“屏幕保护器中的img/prod/2”,
“折扣”:50,
“数量”:1
},
{
“产品id”:3,
“产品名称”:“3合1 USB充电电缆”,
“产品价格”:199,
“产品img”:“img/prod/usb\u charging\u cable.png”,
“折扣”:50,
“数量”:1
}
];
});代码>
我发现了程序的错误
[INFO:CONSOLE(20434)] "Error: [$compile:ctreq] Controller 'ngModel', required by directive 'ngChange', can't be found!
http://errors.angularjs.org/1.3.13/$compile/ctreq?p0=ngModel&p1=ngChange
很抱歉没有这么快说。原因是我没有在
内的
中放置ng型号。所以我把它改成:
<input type="number"
class="mini-input"
style="margin-top: 9px"
ng-change="alert('quantity_inputted');"
ng-model-options="{debounce:1000}"
ng-model="cart.quantity">
我发现了程序的错误
[INFO:CONSOLE(20434)] "Error: [$compile:ctreq] Controller 'ngModel', required by directive 'ngChange', can't be found!
http://errors.angularjs.org/1.3.13/$compile/ctreq?p0=ngModel&p1=ngChange
很抱歉没有这么快说。原因是我没有在
内的
中放置ng型号。所以我把它改成:
<input type="number"
class="mini-input"
style="margin-top: 9px"
ng-change="alert('quantity_inputted');"
ng-model-options="{debounce:1000}"
ng-model="cart.quantity">