Angularjs ng-repeat没有刷新更新的值

时间:2015-11-03 11:11:31

标签: javascript angularjs

我有一个在弹出窗口(ngdialog)上插入和更新记录的功能。当我添加和更新记录正在数据库中插入和更新但这些更新的值没有使用ng-repeat绑定。

只有在刷新整个页面时才会显示更新的记录。

控制器代码:

 $scope.OpenUserPlaceMap = function (userplace) {
    if (angular.isObject(userplace)) {
        $scope.UserPlaceID = userplace.Id;
        $scope.UserPlaceName = userplace.Name;
        $scope.FormattedAddress = userplace.FormattedAddress;
    }
    else {
        $scope.UserPlaceID = '';
        $scope.UserPlaceName = '';
        $scope.FormattedAddress = '';
    }
    ngDialogAlert.ShowPopup('UserPlacesMap.html', 'CommanCtrl', $scope);
};

$scope.SubmitUserPlace = function (flg) {
    $scope.error = [];

    var UserPlace = {
        "Id": $scope.UserPlaceID,
        "UserId": $scope.UserId,
        "Name": $scope.UserPlaceName,
        "FormattedAddress": $scope.FormattedAddress,
        "CircleID": $scope.DefaultCircle,
        "Latitude": $scope.Latitude,
        "Logitude": $scope.Logitude,
        "Flag": flg
    };
    SWT360Service.InUpdDelUserPlace(UserPlace).success(function (data) {
        if (data > 0) {

            ngDialogAlert.ClosePopup();
            $scope.error.push('Saved Successfully');
            ngDialogAlert.ShowAlert(true, $scope.error);
            $scope.LoadUserPlaces();
        }
        else {
            $scope.error.push('User Place not saved Successfully !');
        }
    }).error(function (data) {
        $scope.error.push(data);
    });
}
 $scope.LoadUserPlaces = function () {
    $scope.UserPlaces = '';
    SWT360Service.GetUserPlace(null, $scope.UserId, $scope.DefaultCircle).success(function (data) {
            $scope.UserPlaces = data;
    });
};

查看:

 <div class="container" ng-repeat="place in UserPlaces" style="cursor:pointer;" ng-click="OpenUserPlaceMap(place);">
                    <div class="col-lg-12 bg-success text-center">
                        <i class="fa fa-map-marker"></i> <span style="display:none;">{{place.id}}</span> <span>{{place.Name}}</span> <br />
                        <span>{{place.FormattedAddress}}</span><br>
                    </div>
                    <div class="clearfix"></div>
                    <hr>
                </div>

1 个答案:

答案 0 :(得分:0)

尝试在UserPlaces上添加一个观察者,如下所示,并将所有内容包装在其中。 $ watch观看并更新范围。

$scope.OpenUserPlaceMap = function (userplace) {
    if (angular.isObject(userplace)) {
        $scope.UserPlaceID = userplace.Id;
        $scope.UserPlaceName = userplace.Name;
        $scope.FormattedAddress = userplace.FormattedAddress;
    }
    else {
        $scope.UserPlaceID = '';
        $scope.UserPlaceName = '';
        $scope.FormattedAddress = '';
    }
    ngDialogAlert.ShowPopup('UserPlacesMap.html', 'CommanCtrl', $scope);
};

$scope.$watch('UserPlaces', function(UserPlaces) {

    $scope.SubmitUserPlace = function (flg) {
        $scope.error = [];

        var UserPlace = {
            "Id": $scope.UserPlaceID,
            "UserId": $scope.UserId,
            "Name": $scope.UserPlaceName,
            "FormattedAddress": $scope.FormattedAddress,
            "CircleID": $scope.DefaultCircle,
            "Latitude": $scope.Latitude,
            "Logitude": $scope.Logitude,
            "Flag": flg
        };
        SWT360Service.InUpdDelUserPlace(UserPlace).success(function (data) {
            if (data > 0) {

                ngDialogAlert.ClosePopup();
                $scope.error.push('Saved Successfully');
                ngDialogAlert.ShowAlert(true, $scope.error);
                $scope.LoadUserPlaces();
            } else {
                $scope.error.push('User Place not saved Successfully !');
            }
        }).error(function (data) {
            $scope.error.push(data);
      });
    }
    $scope.LoadUserPlaces = function () {
        $scope.UserPlaces = '';
        SWT360Service.GetUserPlace(null, $scope.UserId, $scope.DefaultCircle).success(function (data) {
            $scope.UserPlaces = data;
        });
    };
});