gpt4 book ai didi

javascript - 根据http请求动态更新 Angular View

转载 作者:行者123 更新时间:2023-12-02 14:20:42 25 4
gpt4 key购买 nike

我是 Angular 的初学者,我正在尝试根据 http 请求更新 View 。

$scope.update = function(id){
switch (id) {
case 1:
$http.get(Backand.getApiUrl() + '/1/objects/details')
.then(function(response) {
$scope.details = response.data.data[0];
$rootScope.locName = response.data.data[0].locationName;
$rootScope.locSubTitle = response.data.data[0].locationSubTitle;
$rootScope.locParagraph = response.data.data[0].locationParagraph;
$rootScope.locGluten = response.data.data[0].glutenfrei;
$rootScope.locRaucher = response.data.data[0].raucher;
$rootScope.locGarten = response.data.data[0].garten;
$rootScope.locTakeAway = response.data.data[0].takeaway;
$rootScope.locRollstuhl = response.data.data[0].rollstuhl;
console.log($rootScope.locGluten);
});
break;
case 2:
$http.get(Backand.getApiUrl() + '/1/objects/details')
.then(function(response) {
$rootScope.locName = response.data.data[1].locationName;
$rootScope.locSubTitle = response.data.data[1].locationSubTitle;
$rootScope.locParagraph = response.data.data[1].locationParagraph;
$rootScope.locGluten = response.data.data[1].glutenfrei;
$rootScope.locRaucher = response.data.data[1].raucher;
$rootScope.locGarten = response.data.data[1].garten;
$rootScope.locTakeAway = response.data.data[1].takeaway;
$rootScope.locRollstuhl = response.data.data[1].rollstuhl;

});
break;
default:

}
}


$scope.goToDetail = function(id) {
$state.go("detail")
$scope.update(id);

}

HTML

<div ng-repeat="cat in content" class="animated lightSpeedIn">
<a ng-click="goToDetail(cat.id)" nav-transition="none"><div ng-style="{'background': 'url(' + cat.catBgUrl + ')','background-repeat': 'no-repeat','background-size': '100% 100%','display': 'block','width': '100%','height': '25vh' }" class="bgcat center">
<div class="inner">
<h1>{{cat.catName}}</h1>
<h4>{{cat.catSubtitle}}</h4>
<img src="img/home/open.png" alt="">
</div>
</div></a>
</div>

我知道这是错误的代码,但我的想法是转到状态详细信息并使用条目的 id 运行函数更新。然后通过 id 进行切换并根据单击的元素更新 View 。

代码工作正常,但我认为有更好的方法来做到这一点,并且我动态地需要它。

我真的很坚持这个,并且想正确地做到这一点。应该怎样制作呢?您能给我一点提示吗?

最佳答案

for codes relating to data it is better to use services for separation.

**Your service**

app成为你的 Angular 模块

app.service('detailService',function($http){
var update = function(id,callback){
switch (id) {
case 1:
$http.get(Backand.getApiUrl() + '/1/objects/details')
.then(function(response) {
callback(response,0);
});
break;
case 2:
$http.get(Backand.getApiUrl() + '/1/objects/details')
.then(function(response) {
callback(response,1);
});
break;
};
});

**Your controller**

app.controller('myCtrl',function($scope,$state,detailService){
detailService.update(id,function(response,index){
$scope.response = response;
$scope.index = index;
$state.go("detail"):
});
});

**You can display the response as**

<p>Details : {{response.data.data[{{index}}]}}</p>
<p>Location Name : {{response.data.data[{{index}}].locationName}}</p>

关于javascript - 根据http请求动态更新 Angular View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38666752/

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