gpt4 book ai didi

javascript - Angular 就绪功能不起作用

转载 作者:行者123 更新时间:2023-11-30 15:30:17 25 4
gpt4 key购买 nike

我有两个 Controller 具有相同的数据,具体取决于语言选择 Controller 将加载。当单击列表项上的项目时正在加载但问题是当其他项目被单击时 Angular 就绪功能不起作用并且我看到以前加载的项目只有

   .controller('EnCtrl', function($scope,sharedCartService) {
var cart = sharedCartService.cart;
$scope.menu_items=[
{"p_id":"1",
"p_name":"coffe ",
"p_image_id":"coffe",
"p_price":"50"
},
{"p_id":"2",
"p_name":"Tea",
"p_image_id":"Tea",
"p_price":"50"
},
{"p_id":"3",
"p_name":"Masala",
"p_image_id":"Masala",
"p_price":"50"
}];


$scope.showProductInfo=function (id,img,name,price) {
sessionStorage.setItem('product_info_id', id);
sessionStorage.setItem('product_info_img', img);
sessionStorage.setItem('product_info_name', name);
sessionStorage.setItem('product_info_price', price);
window.location.href = "#";
};


$scope.addToCart=function(id,image,name,price){
cart.add(id,image,name,price,1);
};
})

Ctrl

.controller('kCtrl', function($scope,sharedCartService) {
var cart = sharedCartService.cart;

$scope.menu_items=[
{ "p_id": "1",
"p_name": "ಕಾಫಿ",
"p_image_id": "coffe",
"p_price": "50"
},
{ "p_id": "2",
"p_name": "ಚಹಾ",
"p_image_id": "Tea",
"p_price": "50"
},
{ "p_id": "3",
"p_name": "ಮಸಾಲಾ",
"p_image_id": "Masala",
"p_price": "50"
}];

$scope.showProductInfo=function (id,img,name,price) {
sessionStorage.setItem('product_info_id', id);
sessionStorage.setItem('product_info_img', img);
sessionStorage.setItem('product_info_name', name);
sessionStorage.setItem('product_info_price', price);
window.location.href = "#";
};


$scope.addToCart=function(id,image,name,price){
cart.add(id,image,name,price,1);
};
})

产品总监

.controller('productCtrl', function($scope) {
//onload event
angular.element(document).ready(function () {
$scope.id= sessionStorage.getItem('product_info_id');
$scope.img= "img/"+ sessionStorage.getItem('product_info_img')+".jpg";
$scope.name= sessionStorage.getItem('product_info_name');
$scope.price= sessionStorage.getItem('product_info_price');
});

中文.html

 <ion-list ng-repeat="item in menu_items"  >
<h2 ng-click="showProductInfo(item.p_id,item.p_image_id,item.p_name,item.p_price)" > {{item.p_name}} </h2>
<p ng-click="showProductInfo(item.p_id,item.p_image_id,item.p_name,item.p_price)">{{item.p_price}}</p>
</ion-list>

Productpage.html

<div>
<p> {{name}}</p>
<p>{{price}}</p>
</div>

评论后更新。单击项目时显示数据更新消息。未获取项目信息

.

controller('EnCtrl', function($scope,sharedCartService,$rootScope) {
var cart = sharedCartService.cart;
$scope.menu_items=[
{"p_id":"1",
"p_name":"coffe ",
"p_image_id":"coffe",
"p_price":"50"
},
{"p_id":"2",
"p_name":"Tea",
"p_image_id":"Tea",
"p_price":"50"
},
{"p_id":"3",
"p_name":"Masala",
"p_image_id":"Masala",
"p_price":"50"
}];


$scope.showProductInfo=function (id,img,name,price) {
$rootScope.$broadcast('dataUpdate', {id: id,img: img,name: name,price: price})
window.location.href = "#";
};


$scope.addToCart=function(id,image,name,price){
cart.add(id,image,name,price,1);
};
})

产品总监

.controller('productCtrl', function($scope,$rootScope) {
$rootScope.$on('dataUpdate', function(data) {
$scope.id = data.id;
$scope.img = data.img;
$scope.name = data.name;
$scope.price=data.price;
})
})

Plunker 演示 https://plnkr.co/edit/zhvBzOzDtgOiTEGjBqAp?p=preview

最佳答案

@user3698911,除了@Bartek Fryzowicz 的回答,

您可能还想看看:

angular.element

Angular 注释:

(deprecated, use angular.element(callback) instead of angular.element(document).ready(callback))

关于javascript - Angular 就绪功能不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42363439/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com