我希望制作自己的网页元素 - 例如,这是一个简单的图形元素:
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);
});
}
};
});
当我在我的页面中声明它时,我必须使用
<html ng-app="myApp">
是否可以将其安装为默认指令,以便我可以仅包含 .js 文件并开始使用 html 元素而无需指定“myApp”名称 - 与所有ng
指令相同?