2015-01-04 33 views
0

通常,我創建了一個控制器,其使用$scope語法,所以,我可以通過現有的$範圍的模態指令的一個隔離的範圍如下:Angular-ui modal:如何使用相同的控制器?

$scope.openEditModal = function() { 
    $scope.modalInstance = $modal.open({ 
     templateUrl: 'views/budgets/mainbudgets/edit', 
     scope: $scope // Passing a $scope variable 
    }); 

    $scope.modalInstance.close(); 
}; 

然而,我剛切換控制器使用this語法:

var self = this; 
// User edit modal 
this.openEditModal = function() { 
    self.modalInstance = $modal.open({ 
     templateUrl: 'views/budgets/mainbudgets/edit', 
     scope: self; // This doesn't work 
    }); 

    self.modalInstance.close(); 
}; 

所以,我怎麼能流過電流this在模態指令隔離範圍內使用?

編輯

這裏是我的控制器的整個代碼:

angular.module('sms.budgets').controller('BudgetsMainController', ['$scope', 'Global', '$modal', '$timeout', '$rootScope','Budgets', function($scope, Global, $modal, $timeout, $rootScope,Budgets) { 
    var self = this; 
    this.newBudget = {}; 
    this.budgets = []; 

    function init() { 
     var data = {}; 

     // Load main budget from DB 
     Budgets.load('main-budgets').success(function(budgets) { 
      self.budgets = budgets || []; 
     }); 
    } 
    init(); 

    /** 
    * Create a new main budget 
    */ 
    this.create = function() { 
     var data = {}; 
     data.budget = self.newBudget; 
     data.dbName = 'Budget'; 
     Budgets.create('budgets', data).success(function() { 
      self.isSuccess = true; 
      $timeout(function() { 
       self.isSuccess = false; 
      }, 5000); 
     }).error(function(err) { 
      self.isError = true; 
      $timeout(function() { 
       self.isError = false; 
      }, 5000); 
     }); 
    }; 

    this.edit = function() { 
     self.modalInstance.close(); 
    }; 

    // User edit modal 
    this.openEditModal = function() { 
     var newScope = $rootScope.$new(); 
     newScope.modalInstance = self.modalInstance; 
     newScope.edit = self.edit; 
     self.modalInstance = $modal.open({ 
      templateUrl: 'views/budgets/mainbudgets/edit', 
      scope: newScope 

     }); 

     self.modalInstance.close(); 
    }; 

    this.cancelEditModal = function() { 
     self.modalInstance.dismiss('cancel'); 
    }; 

}]); 
+0

@pixelbits我採用了棱角分明的UI,模態指令。 – lvarayut

+0

自我不是範圍。你不應該將範圍傳遞給範圍屬性嗎? – pixelbits

+0

我正在使用'controller as'語法,爲什麼我沒有$ scope注入到我的控制器。我知道'自我'不是範圍,但我不知道如何使模態指令在這種情況下工作。 – lvarayut

回答

3

您不能將其用作範圍。他們是不同的。由於$ scope是AngularJS的內部變量,因此您必須保留它。

要說明的是,我創建了一個Plunkr(打開控制檯,並看到這一點,$範圍之間的diffence)

http://plnkr.co/edit/DkWQk4?p=preview

反正就是用在不同的範圍一個很好的做法模態控制器。在這裏你展示如何將主控制器和模態控制器之間通信的示例:

MainCtrl

var modalInstance = $modal.open({ 
    templateUrl: 'views/parts/modalUrlImg.html', 
    controller: 'ModalUrlCtrl', 
    resolve: { 
    url: function() { // pass the url to the modal controller 
     return $scope.imageUrl; 
    } 
    } 
}); 

// when the modal is closed, get the url param 
modalInstance.result.then(function (url) { 
    $scope.courses[i].imageUrl = url; 
}); 

莫代爾按Ctrl

.controller('ModalUrlCtrl', function($scope, $modalInstance, url) { 

    $scope.url = url; // get the url from the params 

    $scope.Save = function() { 
    $modalInstance.close($scope.url); 
    }; 

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

    $scope.Clean = function() { 
    $scope.url = ''; 
    }; 
}); 

希望這幫助你,歡呼。

---編輯---

你可以保持控制器的語法。事實上,你必須混合兩種語法,因爲你只能用這個來增加變量和函數,但不能訪問其他範圍的東西,比如$ scope。$ ...

所以,要在你的情況下,只是通過$範圍:

var self = this; 
// User edit modal 
this.openEditModal = function() { 
    self.modalInstance = $modal.open({ 
     templateUrl: 'views/budgets/mainbudgets/edit', 
     scope: $scope; 
    }); 

    self.modalInstance.close(); 
}; 

我試過在更新plunkr和它的工作現在:

http://plnkr.co/edit/DkWQk4?p=preview

+0

感謝您的回覆。我知道'this'和'$ scope'之間的區別。所以,我選擇使用'this','controller as',語法。無論如何,我可以在模態指令中使用相同的控制器嗎?請參閱我編輯的問題,查看我的控制器的整個代碼。 – lvarayut

+0

查看編輯的答案和顯示它現在工作的plunker –

+0

對不起,答案中的鏈接是錯誤的,正確的鏈接是http://plnkr.co/edit/DkWQk4?p=preview。我已經更新了鏈接 –

0

$scope != controller

通過傳遞this$modal.open()您傳遞控制器的參考,而不是規模。改爲嘗試傳遞$scope

+1

感謝您的回覆。我知道'$ scope!= controller'。但是,如果我使用'controller as'語法,我不知道如何在模態指令中使用相同的控制器。 – lvarayut

相關問題