简体   繁体   中英

angularjs can't assign variable to html5 video src

I got an error of Error while interpolating: videos/{{video.name}} with below code :

<div ng-repeat='video in videos'>
        <div class="col-md-3">
        <video controls>
          <source src="videos/{{video.name}}" type="video/mp4">
          Your browser does not support HTML5 video.
        </video>
        {{video.name}} // this worked
        </div>
    </div>

Tried ng-src too but doesn't work. Strange.

Use this filter,

app.filter("trustUrl", ['$sce', function ($sce) {
        return function (recordingUrl) {
            return $sce.trustAsResourceUrl(recordingUrl);
        };
    }]);

HTML

    <video controls>
      <source src="videos/{{video.name | trustUrl}}" type="video/mp4">
      Your browser does not support HTML5 video.
    </video>

Try this :

Html :

<div ng-app="myApp" ng-controller="MyCtrl">
  <div ng-repeat='video in videos'>
        <div class="col-md-3">
        <video controls>
          <source src="videos/{{video.name}}" type="video/mp4">
          Your browser does not support HTML5 video.
        </video>
        {{video.name}} // this worked
        </div>
    </div>
</div>

JS :

var myApp = angular.module('myApp',[]);

myApp.controller('MyCtrl',function($scope) {
    $scope.videos = [
      {"name":"alpha"},
      {"name":"beta"},
      {"name":"gama"}
    ]
});

Working demo!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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