即使变量更新了数据$scope UI 也未更新

UI Not Updating even after $scope variable has updated data

提问人:Venkata 提问时间:4/27/2017 最后编辑:Venkata 更新时间:4/28/2017 访问量:210

问:

我通过一个服务获取我的视图的数据,并使用 angular.copy() 分配给局部变量;

var init = function () {
        $scope.product = angular.copy(EditItemFactory.getSelectedItem());

    }

调用 init 函数后查看我的 UI

$scope.UpdateItem = function (data) {
        console.log(data);
        var Data = DataFactory.UpdateItem(data);

        if (parseInt(Data) != 0) {

            $modalInstance.dismiss('Close');
        } else {
            console.log(Data);
        }
    }

我正在传递 Update 函数以更新项目并存储在 DB 中。

但是在数据库中更新后,数据将在 UI 中更新

这是我的控制器的完整代码:

(function () {

var productEditCtrl = function ($scope, $modalInstance, EditItemFactory, DataFactory) {




    $scope.cancel = function () {
        $modalInstance.dismiss('Close');
    };

    $scope.UpdateItem = function (data) {
        console.log(data);
        var Data = DataFactory.UpdateItem(data);

        if (parseInt(Data) != 0) {

            $modalInstance.dismiss('Close');
        } else {
            console.log(Data);
        }
    }

    var init = function () {
        $scope.product = angular.copy(EditItemFactory.getSelectedItem());

    }

    init();
}
productEditCtrl.$inject = ['$scope', '$modalInstance', 'EditItemFactory', 'DataFactory']
angular.module("SplitWise").controller("productEditCtrl", productEditCtrl)())

我无法将更改绑定到 UI。

注:I仅在更新的数据存储到数据库中后才想要更新 UI

数据工厂代码:

(function(){

var DataFactory = function($http)
{
    var factory = {};




    factory.getAllItems = function()
    {
        return $http.get("http://localhost:8080/GetAllItems")
    }

    factory.DeleteItem = function(Product)
    {

        $http.post("http://localhost:8080/DeleteItem",{"Model":Product.MODEL_NUMBER}).success(function(data){
            return data;
        }).error(function(err)
                {
            return err;
        })
    }

    factory.UpdateItem = function(Product)
    {

        $http.post("http://localhost:8080/UpdateItem",{"Model":Product.MODEL_NUMBER,"Name":Product.NAME,"MSRP":Product.MSRP,"Quantity":Product.QUANTITY}).success(function(Data){
            return Data;
        }).error(function(err){
            return err;
        })
    }
    return factory
}
DataFactory.$inject=['$http']
angular.module('SplitWise').factory("DataFactory",DataFactory)

}())

javascript html angularjs-scope

评论

3赞 tanmay 4/27/2017
共享您的代码。如果它正在进行 HTTP 调用,它将是异步的,因此您不能只是DataFactoryvar Data = DataFactory.UpdateItem(data)
0赞 Venkata 4/28/2017
嗨@tanmay 已更新数据工厂的代码

答:

0赞 nocodename 4/28/2017 #1

$http.post是异步的,因此该函数在从服务器接收之前返回,因此它是 .你要做的是等到它被成功分配,然后对它做一些事情。UpdateItemDataundefinded

var DataFactory = function($http) {

// ...

factory.UpdateItem = function(Product)
    {    
        return $http.post("http://localhost:8080/UpdateItem",{"Model":Product.MODEL_NUMBER,"Name":Product.NAME,"MSRP":Product.MSRP,"Quantity":Product.QUANTITY});
    }

// ...

}

然后使用这样的函数:UpdateItem

$scope.UpdateItem = function (data) {
    console.log(data);
    DataFactory.UpdateItem(data).then(function(resp) {

        if (parseInt(resp.data) != 0) {

            $modalInstance.dismiss('Close');
        } else {
            console.log(resp);
        }
    }, function(err) {
        console.log(err);
    });
}

评论

1赞 Phix 4/28/2017
请注意,已经返回一个延迟的,无需使用 创建一个新的。$http$q.defer()
1赞 nocodename 4/28/2017
哦,是的,没错。它使它变得更加简单。更新了答案。