gpt4 book ai didi

javascript - Angular - 如何在自定义指令中将 url 传递给 ng-src

转载 作者:行者123 更新时间:2023-12-03 06:18:52 25 4
gpt4 key购买 nike

我是 Angular 新手,我正在尝试从此模板中创建自定义指令:

      <img ng-hide="videoPlaying" ng-src="http://i1.ytimg.com/vi/{{ article.external_media[0].video_id }}/maxresdefault.jpg" class="cover">
<youtube-video ng-if="videoPlaying" video-url="article.external_media[0].original_url" player="youtubePlayer" player-vars="playerVars" class="video"></youtube-video>
<div ng-hide="videoPlaying" class="iframe-overlay" ng-click="playVideo(youtubePlayer)">
<img ng-hide="videoPlaying" class="play" src="icons/play.svg"/>
<img ng-hide="videoPlaying" class="playButton" src="icons/playRectangle.svg"/>
</div>

在本例中 youtube-video 是另一个指令,因为我使用的是 youtube-angular-embed包裹。我想知道如何将我的情况 {{article.external_media[0].video_id }} 中的 url 传递给 ng-src 以获取指令中的图像?这是到目前为止我的指令所取得的进展:

angular.module('coop.directives')
.directive('youtubePlayer', function () {
return {
restrict: 'E',
scope: {
videoPlaying: '=videoPlaying',
playVideo = '=playVideo'
},
template : "<img ng-hide='videoPlaying' ng-src='http://i1.ytimg.com/vi/{{ article.external_media[0].video_id }}/maxresdefault.jpg' class='cover'><youtube-video ng-if='videoPlaying' video-url='article.external_media[0].original_url' player='player' player-vars='playerVars' class='video'></youtube-video><div ng-hide='videoPlaying' class='iframe-overlay' ng-click='playVideo(player)'><img ng-hide='videoPlaying' class='play' src='icons/play.svg'/><img ng-hide='videoPlaying' class='playButton' src='icons/playRectangle.svg'/></div>",
link: function (scope, element, attrs, ngModel) {
}
};
});

然后在我看来,我调用指令是这样的:

<youtube-player></youtube-player>

最佳答案

据我了解,您需要将 url 设置为 ngSrc。

您可以在指令中插入额外的文章参数,并使用它在指令的 Controller 上创建 url。

angular.module('coop.directives')
.directive('youtubePlayer', function () {
return {
restrict: 'E',
scope: {
videoPlaying: '=videoPlaying',
playVideo: '=playVideo',
article: '='
},
template : "<img ng-hide='videoPlaying' ng-src='{{myUrl}}' class='cover'><youtube-video ng-if='videoPlaying' video-url='article.external_media[0].original_url' player='player' player-vars='playerVars' class='video'></youtube-video><div ng-hide='videoPlaying' class='iframe-overlay' ng-click='playVideo(player)'><img ng-hide='videoPlaying' class='play' src='icons/play.svg'/><img ng-hide='videoPlaying' class='playButton' src='icons/playRectangle.svg'/></div>",
link: function(scope, element, attrs, ngModel) {
},
controller: function($scope, $element, $attrs, $transclude) {
$scope.myUrl = 'http://i1.ytimg.com/vi/'+$scope.article.external_media[0].video_id+'/maxresdefault.jpg'
}
};

});

所以你现在可以使用你的指令:

<youtube-player video-playing="video" play-video="play" article="article"></youtube-player>

关于javascript - Angular - 如何在自定义指令中将 url 传递给 ng-src,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38955535/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com