Angularjs 角度指令隔离作用域不更新

Angularjs 角度指令隔离作用域不更新,angularjs,angularjs-directive,angularjs-scope,Angularjs,Angularjs Directive,Angularjs Scope,我已经创建了这个角度指令,它在一定程度上起作用 app.directive('albumArt', function(){ return { restrict: 'AE', scope: { 'pictures': '=pictures', 'source' : '@source' }, replace: true, template: '<img ng-src="{{source}}" width="30" heig

我已经创建了这个角度指令,它在一定程度上起作用

app.directive('albumArt', function(){
return {
    restrict: 'AE',
    scope: {
        'pictures': '=pictures',
        'source' : '@source'
    },
    replace: true,
    template: '<img ng-src="{{source}}" width="30" height="30">',

    link: function( scope, element, attrs ){
         angular.forEach( scope.pictures, function( value, key ){
         //console.log( value.album, attrs.album );
         if( value.album == attrs.album ){
          scope.source="data:" + value.mime_type + ";base64," + value.picture;
          //console.log( value.mime_type );
         }
        }); 
      }
    };
});
app.directive('albumArt',function(){
返回{
限制:“AE”,
范围:{
“图片”:“=图片”,
“source”:“@source”
},
替换:正确,
模板:“”,
链接:函数(范围、元素、属性){
角度.forEach(范围、图片、功能(值、键){
//console.log(value.album、attrs.album);
如果(value.album==attrs.album){
scope.source=“data:”+value.mime_type+““base64””+value.picture;
//console.log(value.mime_类型);
}
}); 
}
};
});
通过以下方式进行呼叫:

<album-art pictures='pictures' album='{{album.title}}' source='{{default}}' class="pull-left art"></album-art>

图片是一个数组,相册标题是明显的,源在主应用程序控制器中设置为默认图像

我遇到的问题是,在指令link:function中的forEach循环中找到匹配项时,设置了scope.source,但没有更新模板{{source}}引用


任何帮助都将不胜感激。谢谢

好的,我通过删除隔离作用域并允许指令从父作用域继承来解决这个问题

仅由以下人员发起:

<album-art album='{{album}}'></album-art>

<album-art album='{{album.title}}'></album-art>

取决于应用程序视图。该指令现在如下所示:

app.directive('albumArt', function(){
return {
    restrict: 'AE',
    replace: true,
    template: '<img ng-src="{{source}}" width="30" height="30">',

    link: function( scope, element, attrs ){
        for( i = 0; i <= scope.pictures.length; i++ ){
            if( scope.pictures[i].album == attrs.album ){
                scope.source="data:" + scope.pictures[i].mime_type + ";base64," + scope.pictures[i].picture;
                break;
            }
        };
     }
  };
});
app.directive('albumArt',function(){
返回{
限制:“AE”,
替换:正确,
模板:“”,
链接:函数(范围、元素、属性){

对于(i=0;i它看起来应该可以工作,当它运行时,html实际显示的是什么?我认为当从链接函数中更改本地范围时,您需要调用
范围。$apply
。另外,我对您的forEach完全感到困惑。您是否正在尝试创建多个
标记?因为现在它似乎只做了一件事将相册中最后一张匹配图片的源值引燃到
范围。source
@NewDev完全正确,如果图片数组中有图像,则将ng src设置为此,如果未设置默认图像。我已修复此问题,但无法发布我自己问题的答案。我将编辑此操作。