Javascript 动态添加、链接和呈现指令
我的标记中有一个标记元素Javascript 动态添加、链接和呈现指令,javascript,html,angularjs,Javascript,Html,Angularjs,我的标记中有一个标记元素弹出窗口,我使用相应的指令处理它。 如果我想在不同的地方显示或隐藏更多这样的小部件,我现在需要将所有这些元素放在我的页面标记中,我不确定它们看起来是否干净,这是最好的方法。看起来是这样的: <popup-window></popup-window> <details-window></details-window> <share-widget></share-widget> <twitter-s
弹出窗口
,我使用相应的指令处理它。
如果我想在不同的地方显示或隐藏更多这样的小部件,我现在需要将所有这些元素放在我的页面标记中,我不确定它们看起来是否干净,这是最好的方法。看起来是这样的:
<popup-window></popup-window>
<details-window></details-window>
<share-widget></share-widget>
<twitter-stream></twitter-stream>
<!doctype html>
<html ng-app="myApp">
<head>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.angularjs.org/1.1.2/angular.min.js"></script>
<script type="text/javascript">
var myApp = angular.module('myApp', []);
myApp.controller('MainCtrl', ['$scope', function($scope){
}]);
myApp.directive('twitterStream', function() {
return {
restrict: 'E',
link: function(scope, elem, attrs) {
elem.append('<p>A tweet: ' + Math.random() + '</p>')
}
}
});
myApp.directive('createTwitterStreamButton', ['$compile', function($compile) {
return {
restrict: 'E',
template: '<button ng-click="add()">Add twitter stream</button>',
replace: true,
link: function(scope, elem, attrs) {
scope.add = function() {
var directiveElement = $compile('<twitter-stream></twitter-stream>')(scope);
directiveElement.insertAfter(elem);
}
}
}
}]);
</script>
</head>
<body ng-controller="MainCtrl">
<create-twitter-stream-button></create-twitter-stream-button>
</body>
</html>
可以对我在DOM中动态添加的元素动态运行指令吗?我想使标记干净。您可以使用$compile服务编译包含指令的模板,并将其附加到页面中。也就是说,如果在有人单击“添加twitter流”按钮之前您不想添加
,您可以执行以下操作:
<popup-window></popup-window>
<details-window></details-window>
<share-widget></share-widget>
<twitter-stream></twitter-stream>
<!doctype html>
<html ng-app="myApp">
<head>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.angularjs.org/1.1.2/angular.min.js"></script>
<script type="text/javascript">
var myApp = angular.module('myApp', []);
myApp.controller('MainCtrl', ['$scope', function($scope){
}]);
myApp.directive('twitterStream', function() {
return {
restrict: 'E',
link: function(scope, elem, attrs) {
elem.append('<p>A tweet: ' + Math.random() + '</p>')
}
}
});
myApp.directive('createTwitterStreamButton', ['$compile', function($compile) {
return {
restrict: 'E',
template: '<button ng-click="add()">Add twitter stream</button>',
replace: true,
link: function(scope, elem, attrs) {
scope.add = function() {
var directiveElement = $compile('<twitter-stream></twitter-stream>')(scope);
directiveElement.insertAfter(elem);
}
}
}
}]);
</script>
</head>
<body ng-controller="MainCtrl">
<create-twitter-stream-button></create-twitter-stream-button>
</body>
</html>
var myApp=angular.module('myApp',[]);
myApp.controller('MainCtrl',['$scope',函数($scope){
}]);
myApp.directive('twitterStream',function(){
返回{
限制:'E',
链接:功能(范围、要素、属性){
元素追加('tweet:'+Math.random()+'')
}
}
});
指令('createTwitterStreamButton',['$compile',函数($compile){
返回{
限制:'E',
模板:“添加推特流”,
替换:正确,
链接:功能(范围、要素、属性){
scope.add=函数(){
var directiveElement=$compile(“”)(范围);
directiveElement.insertAfter(elem);
}
}
}
}]);