从不同的控制器在猫鼬更新时重复观察ng内部的变化

Watching for changes inside ng-repeat on mongoose update from different controller

本文关键字:观察 ng 内部 变化 控制器 更新      更新时间:2023-09-26

我遇到了一个非常具体的问题,我想知道有人能帮我解决这个问题。

因此,我有一个部门控制器,它加载mongodb集合,并将其显示在一个带有ng repeat的表中。每个文档都有一个"编辑"链接来修改自己。编辑链接打开一个模式框,它是一个不同的控制器。我从模态控制器发出PUT请求,并成功地更新了服务器上的文档。

问题是,一旦我离开模态框,我的页面就会显示旧数据,直到我刷新页面。有没有一种方法可以"观察"ng重复区域中的特定文档行。我想确保在不同控制器的更新操作之后,立即修改特定的行。我发现一个已经有效的解决方案是重新加载整个页面&控制器(这样整个集合将用GET重新加载,但这将使用额外的资源,而且似乎不是解决问题的正确方法)。

以下是简化的代码(我已经评论了我认为应该发生更改的地方):

department.html:

<tr dir-paginate="department in departments">
    <td>{{department.id}}</td>
    <td>{{department.name}}</td>
    <td>{{department.office}}</td>
    <td>{{department.phone}}</td>        
    <td> <a href="#" ng-Click="modifyDept(department)"> Edit </a> </td>
</tr

部门管理员:

$http({
            method: 'GET',
            url: '/departments'
        }).success(function(data, status, headers, config) {
             $scope.departments = data;
        })
        .error(function(data, status, headers, config) {
             console.log(" Not Doneee "+ status+data+headers+config);
        });
//I was wondering if I could set up a watch in modifyDept?? The modalInstance however creates a new modal with separate controller.
 $scope.modifyDept = function (depID){
 var modalInstance = $uibModal.open({
      templateUrl: 'partials/deptModal.html',
      controller: 'deptModalController',
      resolve: {
        depID: function () {
          return depID;
        }
      }
    });
};

"partials/deptModal.html"的modalController:

$scope.newID = angular.copy(depID);
$scope.cancel = function () {
    $uibModalInstance.dismiss('cancel');
  };
$scope.modify = function () {
    $http({
            method: 'PUT',
            url: '/departments',
            headers: {
                'Content-Type': 'application/json'
            },
            data: {
                newID: $scope.newID
            }
        }).success(function(data, status, headers, config) {
            $uibModalInstance.dismiss('cancel');
             if (status === 200) {
             // I believe something could also be done here to notify the parent controller (departmentController) maybe via a service?
               notificationFactory.info("Successfully updated: "+$scope.newID.name)
            }
        })
        .error(function(data, status, headers, config) {
        $uibModalInstance.dismiss('cancel');
        notificationFactory.error("Error: Status Code "+status+". Contact admin if issue persists.")
        });
  };

这是我第一次使用angular,我有一种预感,我应该使用$watch&工厂/服务,但就是不知道怎么做。。任何形式的提示或建议都将不胜感激。

将scope:$scope添加到部门控制器中$uibModal.open方法中传递的选项

部门管理员:

$scope.modifyDept = function (depID){
 var modalInstance = $uibModal.open({
      templateUrl: 'partials/deptModal.html',
      controller: 'deptModalController',
      scope: $scope,
      resolve: {
        depID: function () {
          return depID;
        }
      }
    });
};

现在,您将可以访问模式控制器中departmentController的作用域。在$scope.modify方法中的success回调中,可以使用新值更新$scope.departments中的department。