gpt4 book ai didi

javascript - 设置选择菜单的选定值

转载 作者:行者123 更新时间:2023-11-27 23:45:54 25 4
gpt4 key购买 nike

我想设置选择菜单的选定值。值来自 $stateParams。所以我使用 ui-router 并选择菜单处于子状态。这是我尝试的方法。

这是可行的,但我无法拉动我。

Javascript:

.controller('nbgCtrl',function  ($scope, MMG, $stateParams) {
var milo = $stateParams.serix;
var musti = $stateParams.klasor;
MMG.adlar.success(function(loHemen, milo, musti) {
var i;
for (i=0; i<loHemen.length; i++) {
if (loHemen[i].seri===milo) {
break;
};

};
console.log(i);
$scope.mangalar = loHemen;
$scope.selManga = $scope.mangalar[0];
});

.factory('MMG', function($http){
var fveg= {};
var url = 'http://api.example.com/manga?callback=JSON_CALLBACK';
fveg.adlar = $http.jsonp(url);
return fveg;
})

HTML:

<select ng-model="selManga" ng-options="manga.seri for manga in mangalar">
<option value="">Select a Manga</option>
</select>

JSON:

[{"seri": "Naruto",
"randomword": [{
"klasor": "138",
"yol": [
]
},
{
"klasor": "300",
"yol": [

]
}
]
},
{
"seri": "One Piece",
"randomword": [
{
"klasor": "137",
"yol": [

]
}
]
}
])

最佳答案

您的 for 循环逻辑并不完全正确。如果循环体被执行,那么您的 success 函数将返回 i(将为 0),并且函数体的其余部分将不会运行。您应该将 if 条件移动到循环内部。

.controller('nbgCtrl',function  ($scope, MMG, $stateParams) {
var milo = $stateParams.serix;
var musti = $stateParams.klasor;
// remove the milo and musti variables in your promise callback
// or if you actually want to have the response code and the headers,
// give them appropriate variable names
MMG.adlar.success(function(loHemen) {
var i, miloMangaInArray;
for (i=0; i<loHemen.length; i++) {
if (loHemen[i].seri===milo) {
miloMangaInArray = loHemen[i];
break;
}
};
console.log('found value or undefined',miloMangaInArray);
$scope.mangalar = loHemen;
$scope.selManga = miloMangaInArray;
});
})

关于javascript - 设置选择菜单的选定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33202942/

25 4 0