简体   繁体   English

角度:将变量传递给指令

[英]Angular: Pass variable to directive

I'm looking to pass a variable from controller to a directive I have created: 我希望将变量从控制器传递给我创建的指令:

HTML 的HTML

<div class="dropup inline-block" ng-repeat="event in video.events">
    <event video="video"></event>
</div>

DIRECTIVE 指示

.directive("event", function() {
    return {
      restrict: "E",
      replace: true,
      scope:{
        video: '=videoObject'
      },
      template: '<a class="btn btn-default event-margin" ng-style="{color: video.iconColor, float: floatEvents}" type="button"  data-title="{{event.title}}"><span class="fa fa-fw {{event.icon}}"></span></a>',
      link: function(scope, elm, attrs) {
        elm
        .on('mouseenter', function() {
          elm.css('background-color', scope.video.color);
          elm.css('color','#FFFFFF');
        })
        .on('mouseleave', function() {
          elm.css('background-color','#FFFFFF');
          elm.css('color', scope.video.color);
        });
      }
    };

The problem is when I add scope in the returned dict attribute it stops to work. 问题是,当我在返回的dict属性中添加范围时 ,它停止工作。

So the idea is to change the color of the element when I pass the mouse on the element, with the value of video.color , a variable in the $scope of the controller. 因此,这个想法是当我将鼠标移到元素上时,使用控制器$ scope中的变量video.color的值来更改元素的颜色。

I have been looking for an answer in other posts but they don't work: 我一直在其他帖子中寻找答案,但它们不起作用:

If video is object then Add video: '=video' , 如果视频是对象,则添加video: '=video'
If video is a string then add video: '@video' 如果视频是字符串,则添加video: '@video'

.directive("event", function() {
    return {
      restrict: "E",
      replace: true,
      scope:{
        video: '=video'
      },
      template: '<a class="btn btn-default event-margin" ng-style="{color: video.iconColor, float: floatEvents}" type="button"  data-title="{{event.title}}"><span class="fa fa-fw {{event.icon}}"></span></a>',
      link: function(scope, elm, attrs) {
        elm
        .on('mouseenter', function() {
          elm.css('background-color', scope.video.color);
          elm.css('color','#FFFFFF');
        })
        .on('mouseleave', function() {
          elm.css('background-color','#FFFFFF');
          elm.css('color', scope.video.color);
        });
      }
    };

Ok, I find a solution, video is a scope variable of the controller. 好的,我找到了一个解决方案,视频是控制器的作用域变量。 Setting scope to false in the returned dict allows the directive to access the scope of the controller. 在返回的dict中将scope设置为false允许指令访问控制器的范围。

In this web is explained: https://www.undefinednull.com/2014/02/11/mastering-the-scope-of-a-directive-in-angularjs/ 该网站中的解释如下: https : //www.undefinednull.com/2014/02/11/mastering-the-scope-of-a-directive-in-angularjs/

When scope is set to “true”, AngularJS will create a new scope by inheriting parent scope ( usually controller scope, otherwise the application's rootScope ). 当scope设置为“ true”时,AngularJS将通过继承父范围(通常是控制器范围,否则是应用程序的rootScope)来创建新范围。 Any changes made to this new scope will not reflect back to the parent scope. 对此新范围进行的任何更改都不会反映回父范围。 However, since the new scope is inherited from the parent scope, any changes made in the Ctrl1 ( the parent scope ) will be reflected in the directive scope. 但是,由于新作用域是从父作用域继承的,因此在Ctrl1(父作用域)中所做的任何更改都将反映在指令作用域中。

When scope is set to “false”, the controller Ctrl1 and directive are using the same scope object. 当scope设置为“ false”时,控制器Ctrl1和指令使用相同的scope对象。 This means any changes to the controller or directive will be in sync. 这意味着对控制器或指令的任何更改都将同步。

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

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