无法调用方法' trustAsResourceUrl'未定义的

时间:2014-05-08 17:23:59

标签: angularjs url directive

我使用angular来显示Youtube的iframe。为了,我已经使用$ sce来信任外部网址,但我发现了一个我无法解决的错误......也许有人可以向我解释

Controller.js

.controller('DetailsCtrl', ['$scope', '$routeParams', '$http',
    function($scope, $routeParams, $http, $sce) {
        $scope.loading = true;
      $scope.url = $sce.trustAsResourceUrl('//www.youtube.com/embed/4wOoLLDXbDY');
        $http.get('projects/' + $routeParams.projectId + '.json').success(function(data) {
          $scope.project = data;
          $scope.loading = false;
        });
    }]);

Details.html

<iframe width="560" height="315" ng-src"{{url}}" frameborder="0" allowfullscreen></iframe>

控制台错误

TypeError: Cannot call method 'trustAsResourceUrl' of undefined
    at new <anonymous> (http://localhost:9000/scripts/controllers/details.js:22:25)
    at invoke (http://localhost:9000/bower_components/angular/angular.js:3869:17)
    at Object.instantiate (http://localhost:9000/bower_components/angular/angular.js:3880:23)
    at http://localhost:9000/bower_components/angular/angular.js:7134:28
    at link (http://localhost:9000/bower_components/angular-route/angular-route.js:913:26)
    at nodeLinkFn (http://localhost:9000/bower_components/angular/angular.js:6579:13)
    at compositeLinkFn (http://localhost:9000/bower_components/angular/angular.js:5986:15)
    at publicLinkFn (http://localhost:9000/bower_components/angular/angular.js:5891:30)
    at boundTranscludeFn (http://localhost:9000/bower_components/angular/angular.js:6005:21)
    at controllersBoundTransclude (http://localhost:9000/bower_components/angular/angular.js:6600:18) <div ng-view="" class="ng-scope">     

2 个答案:

答案 0 :(得分:5)

试试这个。您需要将$sce指定为dependency

.controller('DetailsCtrl', ['$scope', '$routeParams', '$http', '$sce',
    function($scope, $routeParams, $http, $sce) {
        $scope.loading = true;
      $scope.url = $sce.trustAsResourceUrl('//www.youtube.com/embed/4wOoLLDXbDY');
        $http.get('projects/' + $routeParams.projectId + '.json').success(function(data) {
          $scope.project = data;
          $scope.loading = false;
        });
    }]);

答案 1 :(得分:0)

您必须在控制器中输入$Sec

.controller('DetailsCtrl', ['$scope', '$routeParams', '$http', '$sce',
    function($scope, $routeParams, $http, $sce)

$sce