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