简体   繁体   English

如何在angularjs中将一个指令的属性值赋给另一个指令?

[英]how to get the one directive atttribute value into to another directive in angularjs?

For example: 例如:

  <directive-main>
       <directive-sub1 att-name="test"></directive-sub1>
       <directive-sub2></directive-sub2>
      </directive-main>




JS Source Code


bosAppModule.directive('directive-main',['controlBindingFactory', function(controlBindingFactory) {

    var layouttablecellcontrol={};

    linkLayouttablecellcontrol=function(scope, element, attributes, controllerCtrl) {
        scope.controlData="NO CONTROL DATA";
        angular.forEach(scope.mapperData.collections.controltype.rowset, function (value, index) {
            if(value.controltypeid==scope.controlId){
                scope.controlData=value.controltypename;
            }
        });
    };

    layouttablecellcontrol.scope={controlId:'=',layoutData:'=',pageObject:'=',mapperData:'='};
    layouttablecellcontrol.restrict='AE';
    layouttablecellcontrol.replace='true';
    layouttablecellcontrol.template="<div class='row' ng-repeat='tablecellcontrol in layoutData.collections.layouttablecellcontrol.rowset' " +
                                    "ng-if='tablecell.cellid==tablecellcontrol.layouttablecellcontrolcellid' " +
                                    "control-id='tablecellcontrol.layouttablecellcontrolcontroltypeid' " +
                                    "layout-data='layoutData' " +
                                    "page-object='pageObject' " +
                                    "mapper-data='mapperData'> " +
                                    "<directive-sub1></directive-sub1>" +
                                    "<directive-sub2></directive-sub2>" +                                               
                                    "</div>";

    layouttablecellcontrol.link = linkLayouttablecellcontrol;

    return layouttablecellcontrol;  
}]);

bosAppModule.directive('directive-sub1',function($compile){
    var layoutTableCellControlLabelObj={};

    linkFnTableCellControlLabel=function(scope, element, attributes, controllerCtrl) {
        scope.labelData="NO DATA";
        angular.forEach(scope.pageObject.collections.objectattribute.rowset, function (value, index) {
            if(value.objectattributeid==scope.attributeId){
                scope.labelData=value.objectattributelabelname;
                scope.attributeName=value.objectattributename;
            }
        });
    };

    layoutTableCellControlLabelObj.scope={attributeId:'=',layoutData:'=',pageObject:'='};
    layoutTableCellControlLabelObj.restrict='AE';
    layoutTableCellControlLabelObj.replace='true';
    layoutTableCellControlLabelObj.template="<div class='col-xs-12 col-sm-12 col-md-6 col-lg-6' attribute-name={{attributeName}} attribute-id='tablecellcontrol.layouttablecellcontrolbindingobjectattributeid' " +
                                            "layout-data='layoutData' page-object='pageObject'><label class='k-label pull-right'>{{labelData}}</label></div>";

    layoutTableCellControlLabelObj.link = linkFnTableCellControlLabel;

    return layoutTableCellControlLabelObj;  
});

bosAppModule.directive('directive-sub2',['$compile','layoutRenderingControlsFactory','$parse',function($compile,layoutRenderingControlsFactory,$parse){
    var layoutTableCellControlRenderObj={};

    linkFnTableCellControlRender=function(scope, element, attributes, controllerCtrl) {
        scope.controlData="NO CONTROL DATA";
        var controlContent="";

        /*angular.forEach(scope.pageObject.collections.objectattribute.rowset, function (value, index) {
            if(value.objectattributeid==scope.attributeId){         
                scope.attributeName=value.objectattributename;
            }
        });*/

        scope.$watch(attributes.layoutTableCellControlLabelRender, function(value){
            console.log(value);
        });

        angular.forEach(scope.mapperData.collections.controltype.rowset, function (value, index) {
            if(value.controltypeid==scope.controlId){
                scope.controlData=value.controltypename;
                controlContent=angular.element(layoutRenderingControlsFactory[scope.controlData](scope.controlId, scope.attributeName));
                $compile(controlContent)(scope);
                element.append(controlContent);
            }
        }); 
    };
    layoutTableCellControlRenderObj.scope={controlId:'=',layoutData:'=',pageObject:'=',mapperData:'=', cellControlId:'='};
    layoutTableCellControlRenderObj.restrict='AE';
    layoutTableCellControlRenderObj.replace='true';
    layoutTableCellControlRenderObj.template="<div class='col-xs-12 col-sm-12 col-md-6 col-lg-6' ng-attr-id={{cellControlId}} cell-control-id='tablecellcontrol.layouttablecellcontrolcellcontrolid' " +
                                            "control-id='tablecellcontrol.layouttablecellcontrolcontroltypeid' " +
                                            "layout-data='layoutData' page-object='pageObject' mapper-data='mapperData'></div>";
    layoutTableCellControlRenderObj.link = linkFnTableCellControlRender;

    return layoutTableCellControlRenderObj; 
}]);

In this example having three directives i need to get the directive-sub1 att-name value to directive-sub2. 在这个具有三个指令的示例中,我需要将指令-sub1的att-name值获取到指令-sub2。 Actually i tried using $rootScope also is not working. 实际上,我尝试使用$ rootScope也无法正常工作。

Please help me to achieve this. 请帮助我实现这一目标。 I hope explained well. 我希望能解释清楚。

The best way to share data between directives or controllers in angular its to use a factory, here its a simple example to share data between 2 directives. 最好在指令或控制器之间共享数据的最佳方式是使用工厂,这是在两个指令之间共享数据的简单示例。

HTML: HTML:

<div ng-app="myApp">
    <div ui-foo>I should change iamfoo</div>
    <br>
    <div ui-bar att-label="HELLO">I should change to iambar</div>
</div>

JS: JS:

angular.module('ui.directives', []).directive('uiBar',
    function($parse,Data) {
      return {
        restrict: 'EAC',
        require: '?ngModel',
        link: function($scope, element, attrs, controller) {
          var controllerOptions, options;
          element.text(attrs.attLabel);
          Data.setDataDirectives(attrs.attLabel);
        }
      };
    }
  )
  .directive('uiFoo', 
    function(Data) {
      return {
        restrict: 'EAC',
        require: '?ngModel',
        link: function($scope, element, attrs, controller) {
          var controllerOptions, options;
          var changeattr = function() {
            element.text('iamfoo 4324242'+Data.getDataDirectives())
          }
              $scope.$watch(function(){return Data.getDataDirectives();},function(newValue,oldValue) {
           changeattr();
          })
        }
      };
    }
  );

angular.module('ui.factory',[]).factory('Data',
function() {
var dataDirectives;
return {
    setDataDirectives: function(data){
        dataDirectives = data;
  },
  getDataDirectives: function() {
    return dataDirectives;
  }
}
})
angular.module('myApp', ['ui.directives','ui.factory']);

You can check my Jsfiddle with this example working: http://jsfiddle.net/javierif/g1xjfbb7/ 您可以通过以下示例工作来检查我的Jsfiddle: http : //jsfiddle.net/javierif/g1xjfbb7/

VERSION 2 版本2

If u only want assign value to scope and use this in another directive, im still thinking the best way its use a factory to make this abstract, but u can assign value to scope without factory: 如果您只想将值分配给作用域并在另一个指令中使用它,我仍在思考使用工厂来制作此抽象的最佳方法,但是您可以在没有工厂的情况下将值分配给作用域:

HTML: HTML:

<div ng-app="myApp">
    <div ui-foo>I should change iamfoo</div>
    <br>
    <div ui-bar att-label="HELLO">I should change to iambar</div>

</div>

JS: JS:

angular.module('ui.directives', []).directive('uiBar',
    function($parse,Data) {
      return {
        restrict: 'A',

        link: function($scope, element, attrs, controller) {
          var controllerOptions, options;
          element.text("iambar " + attrs.attLabel);
          $scope.label = attrs.attLabel;
        }
      };
    }
  )
  .directive('uiFoo', 
    function(Data) {
      return {
        restrict: 'A',
        link: function($scope, element, attrs, controller) {
          var controllerOptions, options;
             $scope.$watch(function(){return $scope.label;},function(newValue,oldValue) {
           element.text("iamfoo " + $scope.label)
          })
        }
      };
    }
  );
angular.module('myApp', ['ui.directives']);

And here its my fiddle with this example: http://jsfiddle.net/98L2mfcm/1/ 这是我用这个例子摆弄的东西: http : //jsfiddle.net/98L2mfcm/1/

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

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