Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AngularJS-如何通过attrs将不同的$scope属性传递给指令_Angularjs_Angularjs Directive - Fatal编程技术网

AngularJS-如何通过attrs将不同的$scope属性传递给指令

AngularJS-如何通过attrs将不同的$scope属性传递给指令,angularjs,angularjs-directive,Angularjs,Angularjs Directive,我试图加载由范围填充的ver=“…”属性选择的副本摘录 app.directive('hymn', function () { return { restrict: 'E', scope:{ }, link: function (scope, element, attrs) { scope.contentUrl = 'content/excerpts/hymn-' + attrs.ver + '.html';

我试图加载由范围填充的
ver=“…”
属性选择的副本摘录

app.directive('hymn', function () {
    return {
        restrict: 'E',
        scope:{ },
        link: function (scope, element, attrs) {
            scope.contentUrl = 'content/excerpts/hymn-' + attrs.ver + '.html';        
                attrs.$observe("ver", function (v) {
                    scope.contentUrl = 'content/excerpts/hymn-' + v + '.html';    
                });
            },
        template: '<div><p ng-include="contentUrl"></p></div>'
    }
})
app.directive('hymm',function(){
返回{
限制:'E',
作用域:{},
链接:函数(范围、元素、属性){
scope.contentUrl='content/extracts/hymm-'+attrs.ver+'.html';
属性$观察(“版本”,功能(v){
scope.contentUrl='content/extracts/hymm-'+v+'.html';
});
},
模板:'

' } })
在一个模板中有多个
的实例,每个实例将提取不同的摘录


如果我注释掉
范围:{},
我成功地获得了第一个实例,但在每个
节点上都会重复

作用域:{}
生成一个隔离作用域,因此指令无法访问其中的任何其他内容

看起来您想要的是:

scope: {
  ver: '@'
}
然后将链接函数中的引用更改为
scope.ver
。阅读有关在google上隔离作用域的内容。

我认为您希望通过使用
scope:true
为每个指令使用子
$scope
,而不是使用
scope:{}
隔离
$scope


从父级继承属性将允许您
$eval
指令内
attr.ver
中的字符串。

我怀疑这不会将值
{{scope.date.weekday}
插入
ver
的字符串中。仍然需要访问父作用域才能
$eval
字符串。@ed hinchliffe,我肯定需要进一步阅读。我开始这个项目的时候是从深水区潜入有棱角的海底。不用说它有一些深水区。谢谢你的指导。