search

Home  >  Q&A  >  body text

angular.js - What should I do if the angular directive needs to depend on multiple models?

What should I do if I write a directive that requires multiple models at the same time when I first start using Angular?

仅有的幸福仅有的幸福2841 days ago619

reply all(2)I'll reply

  • 为情所困

    为情所困2017-05-15 16:54:45

    Let me give you a piece of code that I just used for reference:

    directive:

    javascriptangular.module('imageupload', []).directive('myCustomer', function() {
      return {
        restrict: 'A',
        scope: {
            image: '=',
            resizeMaxHeight: '@?',
            resizeMaxWidth: '@?',
            resizeQuality: '@?',
            resizeType: '@?',
            selectedfile: '&onFileSelected'
        },
        link: function postLink(scope, element, attrs, ctrl) {
        }
      };
    });
    

    html

    html<input type="file"
           name="file"
           class="upload"
           id="inputImage2"
           accept="image/*"
           image="image2"
           resize-max-height="300"
           resize-max-width="250"
           resize-quality="0.5" on-file-selected="transferedImage(imageResult)"/>
    

    reply
    0
  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-15 16:54:45

    The person upstairs misunderstood the meaning of the question. He meant that one instruction requires multiple instructions. In fact, it is very simple. Just put the require instruction into an array. The code is as follows:

    app.directive('directveA',function(){})
        .directive('directiveB',function(){})
        .directive('directiveC',function(){
            return {
                require:['directiveA','directiveB']
                link:function(scope,element,attrs,ctrls){
                    var aCtrl=ctrls[0];
                    var bCtrl=ctrls[1];
                    //这样就可以访问依赖指令的控制器了
                }
            };
        });
    

    reply
    0
  • Cancelreply