2016-11-22 232 views
-1

不確定的另一個AngularJS菜鳥需要從別人一點點的見解。

我有一個控制器(staffRenewalsController),其基本定義,當用戶點擊一個錶行來編輯它由$ scope.editRow處理該怎麼做。

它應該存在,使用$ uibModal模態對話框,找到工作和現在的兩個按鈕(Save和Cancel)。

問題:保存()有$ scope可用,而Cancel()有$ scope undefined。我在過去幾天嘗試了很多東西,我不確定爲什麼Cancel()的$ scope是undefined。我將Save()重命名爲任意值,它仍然有$ scope可用。

有人能看看我的縮寫代碼的下方,在一些啓發我在做什麼錯嗎?

 app.controller('staffRenewalsController', ['$scope', '$http', 'uiGridConstants', '$uibModal' , 
      function($scope, $http, uiGridConstants, $uibModal) { 

      $scope.editRow = function(grid, row) { 
       if(row.entity){ 
        debugger 
        $scope.selectedRow = row.entity; 
        $scope.selectedRowBackup = $scope.selectedRow; 
        var renewalNumber = ''; 
        renewalNumber += row.entity.RenewalNo; 
        var modalInstance = $uibModal.open({ 
         animation: true, 
         templateUrl: '@Url.Action("View")' + '?renewalNumber=' + renewalNumber, 
         controller: myCommuteStaffRenewalSingleModalController, 
         size: 'lg', 
         scope: $scope, 
         resolve: { 
          items: function() { 
           debugger 
           return $scope.items; 

          }        , 

         } 
        }); 
       } 
      } 


     var myCommuteStaffRenewalSingleModalController = function($scope, uiGridConstants, $uibModal, $http, $uibModalInstance, items) { 

     debugger 
      $scope.selectedRecord = $scope.$parent.selectedRow; 


      debugger 
      $scope.save = function() { 
       debugger; 

       // $scope is present 

      }; 



      $scope.cancel = function() { 
       debugger; 

       // $scope is undefined...why? 

       $uibModalInstance.dismiss('cancel'); 
      }; 

     };    
+1

它看起來像你的括號沒有按正確的順序關閉。我敢肯定有一個右括號在第6行 –

+0

缺少的是什麼'myCommuteStaffRenewalSingleModalController'以及爲什麼被其內定義你的'myTableController'?另外,'items'解析屬性後面用逗號分隔的是什麼? – Phil

+0

我很抱歉,我試圖刪除那些不相關的代碼,並且爲了公司的原因重命名變量來掩蓋他們的真實姓名。我錯了,現在我已經糾正了。它運行正常,沒有在Chrome中報告的錯誤。感謝您的反饋意見。 – Retroglider

回答

0

Angular Bootstrap Documentation有一個很好的例子,你正在嘗試做什麼。

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; 
    } 
    } 
}); 

通知控制器值在引號controller: 'ModalInstanceCtrl'。 我會試試這個。

+0

OP正在爲模態控制器使用已定義的功能。使用字符串只需從注入器中按名稱獲取控制器,但這顯然不是這種情況。 – Phil