简体   繁体   English

实例化自定义角度控制器

[英]Instantiate custom angular controller

How to instantiate a custom controller from code and preserve scope inheritance. 如何从代码实例化自定义控制器并保留范围继承。 In other words I want to do something like this: 换句话说,我想做这样的事情:

var controller = 'myCtrl';
var html = '<p>{{value}}</p>';
var validScope= $scope.$new({
    value : 'Hello, custom controllers'
}); // Or something like this to get valid scopes inheritance
$(document.body).append(instantiate(controller, html, validScope));

So i need two answers: how to instantiate custom controller and how to do it like angular do. 所以我需要两个答案:如何实例化自定义控制器以及如何像angular do那样做。

UPD . UPD I've tried do it this way: 我试过这样做:

$compile('<div ng-controller="myCtrl">'+html+'</div>')(validScope);

Controller was instantiated. 控制器已实例化。 But placeholded values was not binded. 但是,持有地位的价值并没有结合。

I think that the best approach is to expose a function on the scope for retrieving your controller. 我认为最好的方法是在检索控制器的范围上公开一个函数。 ( ngController can take a string or a function) Lets say you have different values which need different constructors... something vaguely like this: ngController可以带一个字符串或一个函数)让我们说你有不同的值,需要不同的构造函数......有点像这样的东西:

<div ng-repeat="item in items">
  <div ng-controller="controllerFor(item)">
    // whatever
  </div>
</div>

That controllerFor function will know how to do the mapping for you. 那个controllerFor函数将知道如何为你做映射。 Hopefully, you can avoid using $compile all together. 希望你可以避免一起使用$compile

I do not know the context of where you are trying to all this controller from but I am going to assume you are wither in another controller, a service, or a directive. 我不知道你尝试所有这个控制器的位置的上下文,但我会假设你在另一个控制器,服务或指令中枯萎。

The code below will show how to create a controller from a service. 下面的代码将说明如何从服务创建控制器。 The example may cover more than what you would need to do but this is a pattern that will work. 该示例可能涵盖的内容超出了您需要执行的操作,但这是一种可行的模式。

Create an abstract controller, this sets the constructor parameters of the controller and insulates the rest of the dependencies. 创建一个抽象控制器,它设置控制器的构造函数参数并隔离其余的依赖项。

module.factory('AbstractCtrl', ['dependencies...', function (dependencies...) {
    var ctrl = function($scope) {
           // Do controller setup.
    };

    return ctrl;
}]);

Now create a controller implementation based on the abstract 现在基于摘要创建一个控制器实现

module.controller('CtrlImpl', ['$scope', 'AbstractCtrl', function ($scope, AbstractCtrl) {
    // Initialize the parent controller and extend it.
    var AbstractCtrlInstance = new AbstractCtrl($scope);
    $.extend(this, AbstractCtrlInstance);
    // … Additional extensions to create a mixin.
}]);

Now that you have a controller with a minimally defined constructor to create an instance of the controller you just need to call inject the $controller and do the following: 既然你有一个带有最小定义构造函数的控制器来创建一个控制器实例,你只需要调用注入$ controller并执行以下操作:

$controller('CtrlImpl', {$scope: $scope}));

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

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