简体   繁体   English

如何使角度指令仅在一个控制器中工作

[英]How to make an angular directive to work only in one controller

i have many controllers and i have a directive. 我有很多控制器,我有一条指令。 and now i want this directive to work in only one particular controller. 现在我希望此指令仅在一个特定的控制器中工作。

Plunker Link: http://plnkr.co/edit/onDmKl?p=preview Plunker链接: http ://plnkr.co/edit/onDmKl?p=preview

JS: JS:

var app = angular.module('plunker', []);

app.controller('MainCtrlA', function($scope) {

});
app.controller('MainCtrlB', function($scope) {

});

app.directive('ngElevateZoom', function() {
  return {
    restrict: 'A',
    link: function(scope, element, attrs) {
      element.attr('data-zoom-image', attrs.zoomImage);
      $(element).elevateZoom();
    }
  };
});

$(document).ready(function() {
  $('#native').elevateZoom();
});

HTML: HTML:

<div ng-controller="MainCtrlA">
    <img ng-elevate-zoom ng-src="http://s27.postimg.org/xyoknslhf/blue_bird_wallpaper_small.jpg" zoom-image="http://s27.postimg.org/v5h4f601v/blue_bird_wallpaper.jpg" />
  </div>

  <div ng-controller="MainCtrlB">
    <img ng-elevate-zoom ng-src="http://s27.postimg.org/xyoknslhf/blue_bird_wallpaper_small.jpg" zoom-image="http://s27.postimg.org/v5h4f601v/blue_bird_wallpaper.jpg" />
  </div>

now i want that ngElevateZoom directive to work only in MainCtrlA. 现在,我希望ngElevateZoom指令仅在MainCtrlA中工作。

im no expert in angular, so go easy on me ;) 我没有角度方面的专家,所以请放轻松我;)

A nice way to structure your AngularJS application is to modularize every component, that means you create your controllers , directives , filters , etc. and wrap them into a module : 构建AngularJS应用程序的一种好方法是模块化每个组件,这意味着您可以创建控制器指令过滤器等并将它们包装到模块中

var myApp = angular.module('myApp', ['moduleA', 'moduleB']);

angular.module('moduleA', [])
  .directive('myDirectiveA', function() {})
  .controller('MyCtrlA', function($scope) {});

angular.module('moduleB', [])
  .directive('myDirectiveB', function() {})
  .controller('MyCtrlB', function($scope) {});

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

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