简体   繁体   English

Angular-ui模态:如何使用同一控制器?

[英]Angular-ui modal: How to use the same controller?

Normally, I created a controller which used $scope syntax, so, I could pass a current $scope to a isolated scope of the modal directive as following: 通常,我创建了一个使用$scope语法的控制器,因此,我可以将当​​前的$ scope传递给modal指令的隔离范围,如下所示:

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

    $scope.modalInstance.close();
};

However, I just switched the controller to use this syntax: 但是,我只是切换了控制器以使用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();
};

So, how can I pass a current this to be used in isolated scope of the modal directive? 所以,我怎样才能使电流this在模态指令隔离范围使用?

EDIT 编辑

Here is the whole code of my controller: 这是我的控制器的完整代码:

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

}]);

You can't use this as a scope. 您不能将其用作范围。 They are different. 他们是不同的。 Since $scope is a internal variable of AngularJS you have to keep it as it. 由于$ scope是AngularJS的内部变量,因此必须将其保持不变。

To show that, I've created a Plunkr (open the console and see the diffence between this and $scope) 为了说明这一点,我创建了一个Plunkr(打开控制台,查看它和$ scope之间的区别)。

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

Anyway, is a good practise to use a different scope on the modal controller. 无论如何,在模态控制器上使用其他范围是一个好习惯。 Here you have an example showing how to communicate between the main controller and the modal controller: 这里有一个示例,显示了如何在主控制器和模态控制器之间进行通信:

From the MainCtrl : 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;
});

From the Modal Ctrl : 模式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 = '';
  };
});

Hope this help you, cheers. 希望这对您有所帮助,加油。

--- EDIT --- -编辑-

You can keep the controller as syntax. 您可以将控制器保留为语法。 In fact, you must mix both syntax, since you can only use this to add vars and functions, but not tu access other scope things, such as $scope.$on... 实际上,您必须同时使用这两种语法,因为您只能使用它来添加变量和函数,而不能访问其他作用域的东西,例如$ scope。$ on...。

So, to do that in your case, just pass $scope: 因此,要根据您的情况进行操作,只需传递$ scope即可:

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

    self.modalInstance.close();
};

I've tried in the updated plunkr and it's working now: 我已经尝试了更新的plunkr,现在可以正常工作:

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

$scope != controller $ scope!=控制器

By passing this to $modal.open() you are passing the reference of the controller and not the scope. 通过this传递给$modal.open()您传递的是控制器而不是范围的引用。 Try passing $scope instead. 尝试通过$scope代替。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM