1

I'm learning angular, and have this weird problem: my index includes:

<script src="js/controllers.js"></script>
<script src="js/directives.js"></script>

And I have this code:

angular.module('myApp.directives', []).directive('myYoutube', function($sce) {
return {
    restrict: 'EA',
    scope: { code:'=',add:'=' },
    replace: true,
    template: '<div style="height:400px;"><iframe style="overflow:hidden;height:100%;width:100%" width="100%" height="100%" src ="{{url}}" frameborder="0" allowfullscreen></iframe></div>',
    link: function (scope) {
        console.log('here');
        scope.$watch('code', function (newVal) {
            if ($sce.isEnabled()){
                console.log('and');}
            if (newVal) {
                console.log('there');
                scope.url= $sce.trustAsResourceUrl("http://www.youtube.com/embed/" + newVal);
            }
        });
    }
};

});

When I put it in controllers.js it runs fine. But when I put it in directives.js, I get this error:

GET http://localhost:8000/app/%7B%7Burl%7D%7D 404 (Not Found)

Which points out that {{url}} isn't evaluated. The question is why?

,

1 Answer 1

2

Try ng-src instead:

Using Angular markup like {{hash}} in a src attribute doesn't work right: The browser will fetch from the URL with the literal text {{hash}} until Angular replaces the expression inside {{hash}}. The ngSrc directive solves this problem.

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

2 Comments

It works, thanks. But what bothers me more is why the original code did work when in controllers.js
@user3691475: I don't know how you use it in your controller.js. Using src is buggy due to timing problem. It's recommended to use ng-src

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.