简体   繁体   English

在没有jQuery的情况下在AngularJS中扩展DOM对象的推荐方法?

[英]Recommended way to extend DOM object in AngularJS without jQuery?

I'm using angularJS (without jQuery), lo-dash.js and coffeescript.. 我正在使用angularJS(没有jQuery),lo-dash.js和coffeescript ..

Below is my codes: 下面是我的代码:

      div = document.getElementById('play')
      iframe = document.createElement('iframe');
      iframe.id='iframe_played'
      iframe.width = '420'
      iframe.height = '315'
      iframe.src = './home.html#/video/'+ currentMarker.id
      iframe.frameborder = '0'
      iframe.allowfullscreen = 'true'
      iframe.webkitallowfullscreen = 'true'
      iframe.mozallowfullscreen = 'true'
      div.appendChild(iframe);

which looks a little clumsy.. I know it would be super easy if I could use jQuery but unfortunately I didn't import jQuery becasue AngularJS recommend not to do this.. 我看起来如果我能使用jQuery会非常容易,但是不幸的是我没有导入jQuery,因为AngularJS建议不要这样做。

Does anyone have suggestions about refactoring them? 有没有人建议重构它们? (The best way may be put them all in css.. But the src should be dynamic and allowfullscreen seems not supported by CSS..) Thanks! (最好的方法是将它们全部放在CSS中。但是src应该是动态的,并且CSS不支持allowfullscreen 。)谢谢!

function newElement(name, params){
var out = document.createElement(name);
for(var i in params){
out.setAttribute(i, params[i])
}
return out;
}

var div = newElement('div', {'class': 'myDiv', id: 'divId'}); var div = newElement('div',{'class':'myDiv',id:'divId'});

How about a directive like below ( JSFiddle here ):- 像下面的指令怎么样( 这里是JSFiddle ):-

    todoApp.directive('linkFrame', [function () {
    var directive = {};
        directive.restrict = 'A';
        directive.transclude = 'true';
        directive.scope = { ngModel: '=ngModel'};
    directive.template = '<iframe width="420" height="315" ng-src="{{ngModel}}"></iframe>';

        directive.link = function ($scope, element, attributes) {
        };
    return directive;
}]);

Usage is simple:- 用法很简单:

<div link-frame ng-model="lnk.link"></div>

I think directive should be the right way to go for DOM manipulation and transcluding the child element. 我认为指令应该是进行DOM操作和包含子元素的正确方法。 If you would need more control, you can take a look at $compile . 如果需要更多控制,可以查看$ compile

Please mark as answer if this helps! 如果有帮助,请标记为答案!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM