Javascript 如何扩展angularjs中的默认指令?

Javascript 如何扩展angularjs中的默认指令?,javascript,angularjs,Javascript,Angularjs,我希望制作自己的web元素-例如,这是一个简单的图形元素: var app = angular.module('myApp', []); app.directive('gx', function(){ return { restrict: 'E', transclude: true, replace: true, scope: {w: '@', h: '@'}, template: '<svg class="gx" ng

我希望制作自己的web元素-例如,这是一个简单的图形元素:

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

app.directive('gx', function(){
  return {
    restrict: 'E',
    transclude: true,
    replace: true,
    scope: {w: '@',
            h: '@'},
    template: '<svg class="gx" ng-transclude></svg>',

    controller: function ($scope, $element) {
      var children = $scope.children = [];
      this.addChild = function (c) {
        children.push(c);
      };
    },
    link: function (scope, element, attrs, gxCtrl){
      if (gxCtrl) gxCtrl.addChild(scope);
      scope.$watch('w+h', function(val){
        var w = scope.w||"100%",
            h = scope.h||"100%";
        $(element).attr("height", h).attr("width", w);
      });
    }
  };
});
var-app=angular.module('myApp',[]);
应用指令('gx',函数(){
返回{
限制:'E',
是的,
替换:正确,
作用域:{w:'@',
h:“@”},
模板:“”,
控制器:函数($scope$element){
var children=$scope.children=[];
this.addChild=函数(c){
儿童。推(c);
};
},
链接:函数(范围、元素、属性、gxCtrl){
if(gxCtrl)gxCtrl.addChild(范围);
范围:$watch('w+h',功能(val){
var w=范围。w | |“100%”,
h=范围。h | |“100%”;
$(元素).attr(“高度”,h).attr(“宽度”,w);
});
}
};
});
当我在页面中声明它时,我必须使用

<html ng-app="myApp">


是否可以将其作为默认指令安装,这样我就可以只包含.js文件并开始使用html元素,而无需指定“myApp”名称-与所有
ng
指令的方式相同?

AngularJS的核心功能之一是模块化。您可以在单独的文件中创建指令模块,并向其中添加指令

angular.module('directives',[])
.directive('gx', function(){
  return {/** directive definition object **/ };
});
将模块定义为
directives.js
,将您的应用程序定义为
app.js

<script src="directives.js"></script>
<script src="app.js"></script>

示例:

AngularJS的核心特性之一是模块化。您可以在单独的文件中创建指令模块,并向其中添加指令

angular.module('directives',[])
.directive('gx', function(){
  return {/** directive definition object **/ };
});
将模块定义为
directives.js
,将您的应用程序定义为
app.js

<script src="directives.js"></script>
<script src="app.js"></script>
例如: