1

My Angular App works with

  <script>
    var app = angular.module('MyApp', []);
    app.controller('myCtrl', function ($scope, $sce) {

          $scope.urls = [
            {
              "url": $sce.trustAsResourceUrl("https://www.youtube.com/watch?v=KhzGSHNhnbI")
            },
            {
              "url": $sce.trustAsResourceUrl("https://www.youtube.com/watch?v=OPxeCiy0RdY")
            }
          ]

    });
  </script>

But it doesn't work with

  <script>
      urls = [
            {
              "url":"https://www.youtube.com/watch?v=KhzGSHNhnbI" 
            },
            {
              "url":"https://www.youtube.com/watch?v=OPxeCiy0RdY"          
            }
          ]
  </script>   

  <script>
    var app = angular.module('MyApp', []);
    app.controller('myCtrl', function ($scope, $sce) {

      function myUrl(url) {
          this.url = url;
      }

      $scope = [];
      urls.forEach(function (url, i) {

          $scope.push(new myUrl($sce.trustAsResourceUrl(url)));

      });

    });
  </script>

Update: still doesn't work

 <script>
    var app = angular.module('MyApp', []);
    app.controller('myCtrl', function ($scope, $sce) {

        function myUrl(url) {
            this.url = url;
        }

        $scope.urls = [];
        urls.forEach(function (url, i) {

            $scope.urls.push(new myUrl($sce.trustAsResourceUrl(url)));

        });

    });
  </script>

Error: [$sce:itype] http://errors.angularjs.org/1.4.3/$sce/itype?p0=resourceUrl

6
  • instead of $scope.push, try $scope.urls.push Commented Feb 23, 2018 at 14:31
  • 1
    Instead of $scope = []; use $scope.urls = []; and then push using $scope.urls.push. Commented Feb 23, 2018 at 14:32
  • 1
    also this is utterly wrong: $scope = [];. add a property to $scope, don't re-define it as an array Commented Feb 23, 2018 at 14:32
  • Updated code above with $scope.urls = []; $scope.urls.push but still doesn't work Commented Feb 23, 2018 at 14:39
  • is there any error in the console Commented Feb 23, 2018 at 14:44

1 Answer 1

1

replace $scope.push(new myUrl($sce.trustAsResourceUrl(url))); with $scope.push(new myUrl($sce.trustAsResourceUrl(url.url)));

https://plnkr.co/edit/tpl:FrTqqTNoY8BEfHs9bB0f?p=preview

 urls = [
            {
              "url":"https://www.youtube.com/watch?v=KhzGSHNhnbI" 
            },
            {
              "url":"https://www.youtube.com/watch?v=OPxeCiy0RdY"          
            }
          ]


var app = angular.module('plunker', []);
    app.controller('MainCtrl', function ($scope, $sce) {

      function myUrl(url) {
          this.url = url;
      }

      $scope.urls = [];
      urls.forEach(function (url, i) {

          $scope.urls.push(new myUrl($sce.trustAsResourceUrl(url.url)));

      });

    });
<!DOCTYPE html>
<html ng-app="plunker">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <script data-require="[email protected]" src="https://code.angularjs.org/1.2.28/angular.js" data-semver="1.2.28"></script>
    <script src="app.js"></script>
  </head>

  <body ng-controller="MainCtrl">
    <p ng-repeat="url in urls">Hello {{url.url}}!</p>
  </body>

</html>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.