简体   繁体   English

将角度指令范围传递给指令控制器

[英]Pass angular directive scope to directive controller

I'm currently learning to use directives in AngularJs, and even though I know Angular 2.0 is still just in alpha, I've started reading into "preparing for angular 2.0" articles around the web. 我目前正在学习在AngularJs中使用指令,尽管我知道Angular 2.0仍然只是alpha版本,但我已经开始阅读网络上的“准备angular 2.0”文章。

There's quite a bit of them that mention stepping away from the link function and using the controller and bindToController just to make directives act a bit more like an Angular 2.0 "component". 他们中有很多提到退出链接功能并使用controller和bindToController只是为了使指令的行为更像Angular 2.0“组件”。

The problem I'm having is that I don't really know how to pass my directive scope to the controller I'm using for that directive... 我遇到的问题是我真的不知道如何将指令范围传递给我用于该指令的控制器...
For instance, when given the following directive: 例如,当给出以下指令时:

(function() {
  'use strict';

  angular.module('app').directive('gidsImagePreview', imagePreview);

  /* @ngInject */
  function imagePreview() {
    var directive = {
        restrict: 'EA',
      templateUrl: 'scripts/directives/gidsImagePreview.directive.html',
      scope : {
            images : '='
        },
      controller: ImagePreviewController,
      controllerAs: 'vm',
            bindToController: true
    };

    return directive;
  }

    /* @ngInject */
  function ImagePreviewController(){
        var self = this;
        self.featured = self.images[0];
        self.preview = preview;

        function preview(img){
            self.featured = img;
        }
  }
})();

And the following html to "call" the directive (vm.project.images is an array of image objects with a filename property): 和以下用于“调用”指令的html(vm.project.images是具有filename属性的图像对象数组):

<gids-image-preview images="vm.project.images"></gids-image-preview>

Then, how is it that my "self.images" in the ImagePreviewController is always undefined? 那么,ImagePreviewController中的“ self.images”始终是未定义的怎么办?

Reading this article , he/she seems to be doing exactly what I'm doing (just not with an array object)... 阅读本文 ,他/她似乎完全在执行我的操作(只是不使用数组对象)...

You simply need to inject the scope into the controllers. 您只需要将范围注入控制器即可。 If you aren't minify-ing you cna simply put $scope as an argument to ImagePreviewController : 如果您不缩小,那么只需将$scope作为ImagePreviewController的参数即可:

    function ImagePreviewController($scope) {
        ...
     }

Also if minify-ing is important add: ImagePreviewController.$inject = ['$scope']; 另外,如果最小化很重要,则添加: ImagePreviewController.$inject = ['$scope']; below the controller definition. 低于控制器定义。

If you want to use the array literal syntax, declare it this way: 如果要使用数组文字语法,请按以下方式声明:

    var ImagePreviewController = ['$scope', function ($scope) { ... }];

Then you can access the images array via $scope.images . 然后,您可以通过$scope.images访问images数组。 I prefer using the $inject property, simply as a matter of style. 我更喜欢将$inject属性用作样式。 The this/self workflow you were trying to use is only useful if you want the controller itself to expose an API for other directives to use. 您尝试使用的this / self工作流程仅在您希望控制器本身公开API供其他指令使用时才有用。

For future reference, the angular docs have a pretty complete description of directives and the different ways you can compose them: https://docs.angularjs.org/guide/directive 为便于将来参考,角度文档对指令及其组成方式的描述非常完整: https : //docs.angularjs.org/guide/directive

但是您可以使用[directiveElement] .getIsolatedScope()函数。该函数随处可见。也许此函数可以通过其他方式帮助您解决问题。

As it turns out, nothing was really wrong with what I was doing. 事实证明,我所做的一切都没有错。 It was a timing issue. 这是一个时间问题。
The moment I wanted to use the "images" in my directive controller, it was still undefined because the main object on which my directive was bound (project.images) wasn't loaded yet. 我想在指令控制器中使用“图像”的那一刻,它仍然是未定义的,因为尚未绑定我的指令绑定到的主对象(project.images)。
I've put a resolve function in my routing config to make sure that the main object was loaded before my detailpage (containing the directive) is opened. 我在路由配置中放置了resolve函数,以确保在打开detailpage(包含指令)之前已加载主对象。

Also, just to add to this, @camden_kid mentioned I had to use var vm=this in my controller because I used the controllerAs: "vm" option; 另外,@ camden_kid提到,我不得不在控制器中使用var vm = this,因为我使用了controllerAs:“ vm”选项; as far as I know, this is not true, what you use inside your controller function is irrelevant to how you refer to properties of that controller in your views. 据我所知,这是不对的,您在控制器函数中使用的内容与在视图中引用该控制器的属性无关。
So, using controllerAs: 'vm' only means you will have to use the "vm." 因此,使用controllerAs:'vm'仅意味着您将必须使用“ vm”。 notation in the view. 视图中的符号。 It has nothing to do with what variable you will be using in the controller function (do correct me if this is wrong, but, from what I've learned so far, this is not the case). 它与您将在控制器函数中使用的变量无关(如果这是错误的,请更正我,但是,根据我到目前为止所学的知识,情况并非如此)。

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

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