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
$scope.push, try$scope.urls.push$scope = [];use$scope.urls = [];and then push using$scope.urls.push.$scope = [];. add a property to $scope, don't re-define it as an array