angularjs越来越NG选项OBJ ID而不是价值
<select ng-model="ad.Categorie"
ng-options="obj.id as obj.name for obj in categories"
ng-change="getSubcategories()"
class="form-control"
ng-required="true"
name="categorie">
<option value="">-- --</option>
</select>
当我提出让NG选项数组索引,而不是ad.Categorie价值,我解决了隐藏的输入angularjs越来越NG选项OBJ ID而不是价值
<input type="hidden" name="categorie" value="@{{ad.Categorie}}" />
但似乎不对任何解决方案理念?
var categories = [{"id":1,"name":"cat1"},{"id":2,"name":"cat2"}];
从数据库读取数据。我想要类别ID为fk
public function store(Request $request)
{
$ad = new Ad;
$ad->categorie_id= $request->categorie;
$ad->subcategorie_id= $request->subcategorie;
$ad->type_id= $request->type;
$ad->location_id= $request->location;
$ad->title= $request->title;
$ad->description= $request->description;
$ad->price= $request->price;
$ad->phone= $request->phone;
$ad->code= $request->code;
//$ad->img = $request->file('img');
//$files = $request->file('img');
$ad->save();
return redirect('/');
}
是这个数组$ request-> categorie这就是为什么我得到索引?
你的代码已经得到正确id
,我假设你想要得到的对象或索引。这个代码是用于获取索引ID和对象:
<select ng-model="ad.Categorie"
ng-options="obj.name for obj in categories"
ng-change="getSubcategories()"
class="form-control"
ng-required="true"
name="categorie">
</select>
在你的控制器:
function myCtrl($scope) {
$scope.categories = [{
"id": 11,
"name": "name1"
}, {
"id": 22,
"name": "name2"
}];
$scope.ad = {}
$scope.getSubcategories = function() {
console.log("selected object= ", $scope.ad.Categorie);
console.log("selected index= ", $scope.categories.indexOf($scope.ad.Categorie));
console.log("selected id= ",$scope.ad.Categorie.id);
}
}
这里是工作示例: https://jsfiddle.net/U3pVM/30048/
谢谢你的回答{“id”:1,“name”:“Ажлынзар”}仍然以 –
作为对象,你想获得'id'?那么你的代码工作 – imudin07
是的,我想在这种情况下提出让0没有ID –
可以提供的JSON'categories'。你也可能在你的选择中出现拼写错误(在'name'属性后没有关闭''') – haxxxton
var categories = [{“id”:1,“name”:“name1”},{“id”:2 ,“名称”:“名称2”}]从数据库中读取是的“在代码中是正确的 –
'ng-options =”obj.name for obj in categories“'does not work? – imudin07