Angular-ui模式:如何使用相同的控制器?

时间:2015-01-04 15:25:37

标签: javascript angularjs

通常,我创建了一个使用$scope语法的控制器,因此,我可以将当​​前的$ scope传递给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');
    };

}]);

2 个答案:

答案 0 :(得分:3)

您不能将此作为范围使用。它们是不同的。由于$ scope是AngularJS的内部变量,因此必须保持原样。

为了表明这一点,我创建了一个Plunkr(打开控制台,看看这个和$ scope之间的差异)

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

模态控制

.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。$ on ...

所以,要在你的情况下这样做,只需传递$ 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

答案 1 :(得分:0)

  

$ scope!= controller

通过将this传递给$modal.open(),您传递的是控制器的引用,而不是范围。请尝试传递$scope