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

时间:2016-08-15 12:56:40

标签: javascript angularjs

我是角色新手,我正试图从这个模板中制作一个自定义指令:

      <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 }}中的网址传递给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>

3 个答案:

答案 0 :(得分:1)

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

您可以在指令中插入额外的文章参数,并使用它在指令的控制器上创建网址。

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>

答案 1 :(得分:0)

试试这个:

  scope: {
      videoPlaying: '@videoPlaying',
      playVideo: '@playVideo'
  },

答案 2 :(得分:0)

scope: {
  videoPlaying: '=videoPlaying',
  playVideo: '=playVideo'
  article: '=article'
},

然后您可以在模板中使用它:

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>",

现在在您看来,您可以像这样调用指令:

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

所有这些video, play and article都是绑定到scopecontroller的对象。