$watch函数给出了undefined
$watch function gives undefined - AngularJS
我是一个非常基础的AngulerJS初学者。
我想做的是,当用户点击一个产品时,会出现该产品的类型,然后他们选择一种类型,然后出现该类型的颜色。我试过使用相同的系统来显示类型,但我的颜色一直未定义。
<section class="colorDisplay">
<div class="mcolor" ng-repeat="color in shoColor">
<img ng-src="{{color.image}}"/></div>
</section>
script.js
$scope.prod = {"name":"Pens"};
$scope.typ = {"id":1};
$scope.$watch('prod.name',function(){
$http.get("products/"+$scope.prod.name+".json").success(function(data){
$scope.type = data;
});
$scope.$watch('typ.id',function(type){
$scope.shoColor = type[$scope.typ.id].colors;
});
})
JSON文件
{
"types":[
{
"name":"laser",
"id":0,
"image":"d-u-b/pens/laser.png",
"colors":[
{
"color":"Black and Silver",
"image":"colors/blacksilver.png",
"prodimage":"d-u-b/pens/laser/blacksilver.png"
},
{
"color":"Red",
"image":"colors/red.png",
"prodimage":"d-u-b/pens/laser/red.png"
}
]
},
{
"name":"plain",
"id":1,
"image":"d-u-b/pens/plain.png",
"colors":[
{
"color":"Yellow",
"image":"colors/yellow.png",
"prodimage":"d-u-b/pens/plain/yellow.png"
},
{
"color":"Blue",
"image":"colors/blue.png",
"prodimage":"d-u-b/pens/plain/yellow.png"
},
{
"color":"Cyan",
"image":"colors/cyan.png",
"prodimage":"d-u-b/pens/plain/cyan.png"
},
{
"color":"Silver",
"image":"colors/silver.png",
"prodimage":"d-u-b/pens/plain/silver.png"
}
]
}
]
}
我试着把特定类型的id号命名为颜色,把它写成这样:types[0]。颜色,其中0是插入的id号。无论我把手表功能放在哪里,或者我给它起什么名字,它要么是"0"没有定义,要么是"颜色"没有定义。
我这次做错了什么?change this
$scope.shoColor = type[$scope.typ.id].colors;
$scope.shoColor = $scope.type[type].colors;
和
$http.get("products/"+$scope.prod.name+".json").success(function(data){
$scope.type = data;
$http.get("products/"+$scope.prod.name+".json").success(function(response){
$scope.type = response.data;
这是一个我认为你正在努力实现的工作小提琴。
注意: -我已经硬编码了$scope.type data
,以便它在jsfiddle上工作。
我使用了嵌套的ng-repeat,因为它遵循与json响应相同的代码结构。
希望有帮助,
D相关文章:
- 为什么会出现错误;未捕获的类型错误:undefined不是函数;
- /undefined在我的404错误日志中多次出现
- RequireJS向模块传递配置总是返回undefined
- JSON数组数据返回Undefined
- 为什么会出现错误,“UncaughtTypeError:undefined不是函数”
- Array.lenght = undefined
- mongoose.connect undefined不是一个函数
- JavaScript”;.方法“;返回undefined
- Uncaught TypeError:undefined不是函数-ember js
- Jquery - table.row(tr) is undefined
- jQuery可以't获取data-*属性值,返回undefined
- noty.js is returning undefined
- 将对象的列表c#传递给javascript:undefined变量时出错
- $routeParams is undefined
- angularjs$watch获取已更改数组项的索引
- 试图打印/调用/或提醒数组中的对象:Getting Undefined
- jQuery Mobile Undefined不是一个函数
- 在控制器上使用“$watch”时,为什么不更新此隐藏字段
- $scope.$watch(var,function) undefined 不是函数
- $watch函数给出了undefined