我创建了一个指令,可以为我页面上的组件动态创建 CSS。每次加载页面时,我都会收到错误消息 ReferenceError: youtubeBgVideoStyle is not defined
。这是什么原因?
HTML
<div class="vc_video-bg vc_hidden-xs">
<iframe class="inner" frameborder="0" allowfullscreen="1" allow="autoplay; encrypted-media" title="YouTube video player" width="100%" height="100%" src="https://www.youtube.com/embed/VMwLyWZDJ_4?playlist=VMwLyWZDJ_4&iv_load_policy=3&enablejsapi=1&disablekb=1&autoplay=1&controls=0&showinfo=0&rel=0&loop=1&wmode=transparent&widgetid=1&mute=1" id="widget2" ng-style="youtubeBgVideoStyle()" resize></iframe>
</div>
指令
angular.module('adsomaApp')
.directive('resize', function () {
return function (scope, $window) {
var w = angular.element($window);
scope.getWindowDimensions = function () {
return {
'h': w.height(),
'w': w.width()
};
};
scope.$watch(scope.getWindowDimensions, function (newValue) {
scope.windowHeight = newValue.h;
scope.windowWidth = newValue.w;
scope.youtubeBgVideoStyle = function () {
return {
'max-width': '1000%',
'margin-left': '0px',
'width': newValue.w + 'px',
'min-height': '100vh',
'height': newValue.h + 'px',
'margin-bottom': '-50%'
};
};
}, true);
console.log(youtubeBgVideoStyle());
w.bind('resize', function () {
scope.$apply();
});
};
});
最佳答案
好吧,错误很简单,你正在做:
console.log(youtubeBgVideoStyle());
它应该在哪里:
console.log(scope.youtubeBgVideoStyle());
关于javascript - 引用错误 : youtubeBgVideoStyle is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48719386/