Angularjs src=";{{url}}不';不能在一个文件中工作,但可以在另一个文件中工作。

Angularjs src=";{{url}}不';不能在一个文件中工作,但可以在另一个文件中工作。,angularjs,Angularjs,我正在学习angular,有一个奇怪的问题:我的索引包括: <script src="js/controllers.js"></script> <script src="js/directives.js"></script> 我有这个密码: angular.module('myApp.directives', []).directive('myYoutube', function($sce) { return { restrict:

我正在学习angular,有一个奇怪的问题:我的索引包括:

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

我有这个密码:

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);
            }
        });
    }
};
angular.module('myApp.directives',[]).directive('myYoutube',function($sce){
返回{
限制:“EA”,
作用域:{code:'=',add:'='},
替换:正确,
模板:“”,
链接:功能(范围){
console.log('here');
范围:$watch('code',函数(newVal){
如果($sce.isEnabled()){
console.log('and');}
if(newVal){
console.log('there');
scope.url=$sce.trustAsResourceUrl(“http://www.youtube.com/embed/“+newVal);
}
});
}
};
}))

当我把它放到
controllers.js
中时,它运行良好。但是当我把它放在
directions.js
中时,我得到了以下错误:

获取
http://localhost:8000/app/%7B%7Burl%7D%7D
404(未找到)

它指出,{{url}}没有被计算。问题是为什么

请尝试:

在src属性中使用像{{hash}}这样的角度标记是行不通的 右图:浏览器将从URL获取文本 {{hash}}直到Angular替换了{{hash}内的表达式。这个 ngSrc指令解决了这个问题


很有效,谢谢。但更让我困扰的是,为什么最初的代码在控制器中可以工作。js@user3691475:我不知道你是如何在controller.js中使用它的。由于时间问题,使用
src
是错误的。建议使用ng src