1
我希望做我自己的网页元素 - 比如,这是一个简单的图形元素:如何在angularjs中扩展默认指令?
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
指令相同?
这就是更好比我原本以为!谢谢! – zcaudate