我想为选择菜单设置选定的值。价值来自$ stateParams。所以我使用ui-router并且选择菜单处于子状态。这是我尝试的。
这是有效的,但我不能拉i。
Javascript:
const std::string name = objects[0].get()->name();
CPPUNIT_ASSERT_EQUALS("b", name);
HTML:
.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;
})
JSON:
<select ng-model="selManga" ng-options="manga.seri for manga in mangalar">
<option value="">Select a Manga</option>
</select>
答案 0 :(得分:1)
你的for循环逻辑并不完全正确。如果循环体被执行,那么你的成功函数将返回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;
});
})