2017-08-03 65 views
0

我有一個奇怪的場景,我的數據在模態窗口打開後即被更新。我應該通過在模態彈出窗口中添加一個新項目來刷新模態彈出窗口中的數據。 這裏是蹲跳者,我試圖通過$rootScope,但從文檔中,意識到默認範圍通過是$rootScope。 我plunker鏈接 https://plnkr.co/edit/hnMGHfsxPfq8BRCtVxqJ

我採用了棱角分明的UI引導和利用$uibModal 請提出一個解決方案,我可以試試。 在我的plunker代碼中,即使$ item被更新,我的模式也不會被刷新。

<!doctype html> 
<html ng-app="ui.bootstrap.demo"> 
    <head> 
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.js"></script> 
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular-animate.js"></script> 
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular-sanitize.js"></script> 
    <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-2.5.0.js"></script> 
    <script src="example.js"></script> 
    <link href="//netdna.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> 
    </head> 
    <body> 
<div ng-controller="ModalDemoCtrl as $ctrl" class="modal-demo"> 
    <script type="text/ng-template" id="myModalContent.html"> 
     <div class="modal-header"> 
      <h3 class="modal-title" id="modal-title">I'm a modal!</h3> 
     </div> 
     <div class="modal-body" id="modal-body"> 
      <ul> 
       <li ng-repeat="item in $ctrl.items"> 
        <a href="#" ng-click="$event.preventDefault(); $ctrl.selected.item = item">{{ item }}</a> 
       </li> 
      </ul> 
      Selected: <b>{{ $ctrl.selected.item }}</b> 
     </div> 
     <div class="modal-footer"> 
      <button class="btn btn-primary" type="button" ng-click="$ctrl.ok()">OK</button> 
      <button class="btn btn-warning" type="button" ng-click="$ctrl.cancel()">Cancel</button> 
     </div> 
    </script> 
    <script type="text/ng-template" id="stackedModal.html"> 
     <div class="modal-header"> 
      <h3 class="modal-title" id="modal-title-{{name}}">The {{name}} modal!</h3> 
     </div> 
     <div class="modal-body" id="modal-body-{{name}}"> 
      Having multiple modals open at once is probably bad UX but it's technically possible. 
     </div> 
    </script> 

    <button type="button" class="btn btn-default" ng-click="$ctrl.open()">Open me!</button> 
    <button type="button" class="btn btn-default" ng-click="$ctrl.additems()">Add Item</button> 
    {{ $ctrl.items}} 
    <div class="modal-parent"> 
    </div> 
</div> 
    </body> 
</html> 
    angular.module('ui.bootstrap.demo', ['ngAnimate', 'ngSanitize', 'ui.bootstrap']); 
angular.module('ui.bootstrap.demo').controller('ModalDemoCtrl', function ($uibModal, $log, $document) { 
    var $ctrl = this; 
    $ctrl.items = ['item1', 'item2', 'item3']; 

    $ctrl.animationsEnabled = true; 
    $ctrl.additems = function(){ 
    $ctrl.items.push("item"+($ctrl.items.length+1)); 

    }; 
    $ctrl.open = function (size, parentSelector) { 
    var parentElem = parentSelector ? 
     angular.element($document[0].querySelector('.modal-demo ' + parentSelector)) : undefined; 
    var modalInstance = $uibModal.open({ 
     animation: $ctrl.animationsEnabled, 
     ariaLabelledBy: 'modal-title', 
     ariaDescribedBy: 'modal-body', 
     templateUrl: 'myModalContent.html', 
     controller: 'ModalInstanceCtrl', 
     controllerAs: '$ctrl', 
     size: size, 
     appendTo: parentElem, 
     resolve: { 
     items: function() { 
      return $ctrl.items; 
     } 
     } 
    }); 
    setTimeout(function() { 

      $ctrl.items.push("item"+($ctrl.items.length+1)); 
     }, 1000); 

    modalInstance.result.then(function (selectedItem) { 
     $ctrl.selected = selectedItem; 
    }, function() { 
     $log.info('Modal dismissed at: ' + new Date()); 
    }); 
    }; 



    $ctrl.toggleAnimation = function() { 
    $ctrl.animationsEnabled = !$ctrl.animationsEnabled; 
    }; 
}); 

// Please note that $uibModalInstance represents a modal window (instance) dependency. 
// It is not the same as the $uibModal service used above. 

angular.module('ui.bootstrap.demo').controller('ModalInstanceCtrl', function ($uibModalInstance, items) { 
    var $ctrl = this; 
    $ctrl.items = items; 
    $ctrl.selected = { 
    item: $ctrl.items[0] 
    }; 

    $ctrl.ok = function() { 
    $uibModalInstance.close($ctrl.selected.item); 
    }; 

    $ctrl.cancel = function() { 
    $uibModalInstance.dismiss('cancel'); 
    }; 
}); 

我的問題特別是在這裏,模態與傳遞項目已經呈現。 ctrl.items在1秒後更新,但傳遞項目的模式窗口未更新。有沒有一種方法,我可以送$ ctrl.items的更新模式窗口

setTimeout(function() { 
        $ctrl.items.push("item"+($ctrl.items.length+1)); 
     }, 1000); 
+0

請在問題本身有更詳細的問題描述以及相關的代碼。演示很棒,但只能用於支持問題中實際存在的內容。人們不應該離開現場只是審查你的問題的基礎 – charlietfl

回答

0

你的問題不是很清楚。從你的plunkr中,我發現當原始數據被修改時,模態數據會得到更新。所以,假設這是你的問題:Javascripts對象被引用複製。所以爲了避免這種情況,你需要創建一個對象的新副本。像這樣:

items: function() { 
      return angular.copy($ctrl.items); 
     } 

更新plunkr:https://plnkr.co/edit/pHDr0B0jWRBGeNSPu0t7?p=preview

+0

我道歉,真正的事實是,如果我更新根上的項目,我想模式上的項目被更新,但它不是發生並試圖找出一種方法來發布來自根目錄的更新。 –

+0

發生了。當你打開模式時,點擊一些項目,你會看到範圍得到更新。你需要強制應用() – yBrodsky

+0

是的,謝謝指出,我所做的問題是,我發送項目到我的模態控制器,但我將「items」集合映射到另一個對象。所以,即使我應用這些更改,我的UI也沒有更新。感謝您指點我正確的方向 –