将从web api返回的角度范围对象绑定到下拉列表

时间:2014-12-30 02:53:29

标签: jquery angularjs angularjs-scope

我无法将从服务返回的范围对象绑定到下拉列表(ng-model = ProdCategoryId)以显示为所选项目。我不确定我做错了什么。

Controller.js

 $scope.GetSingleProduct = function (prod) {
    var getsingleprod = ProductService.GetSingleProduct(prod.ProdId);
    getsingleprod.then(function (pl) {
        var singleproddetails = pl.data;
        $scope.ProdCategoryId = singleproddetails[0].ProdCategoryId;
    }, function (err) {
        console.log("Err" + err);
    });

HTML

 <select ng-model="ProdCategoryId" class="form-control"
                            ng-options="item as item.ProdCategoryName for item in catlist|orderBy:'ProdCategoryName'">
                        <option value="">Choose Category</option>
                    </select>

1 个答案:

答案 0 :(得分:0)

最有可能的角度不知道您的模型发生了变化,具体取决于您的服务正在做什么。您需要调用$ scope.apply(),如下所示:

$scope.GetSingleProduct = function (prod) {
var getsingleprod = ProductService.GetSingleProduct(prod.ProdId);
getsingleprod.then(function (pl) {
    var singleproddetails = pl.data;
    $scope.ProdCategoryId = singleproddetails[0].ProdCategoryId;
    $scope.apply()
}, function (err) {
    console.log("Err" + err);
});

这是一篇有用的文章:http://www.sitepoint.com/understanding-angulars-apply-digest/