0

我試圖將我的ng-repeat本地對象傳遞給指令。我需要從我的指令控制器中訪問該對象的數據。我很困惑iso-lated scope和控制器範圍。爲什麼它不起作用。將ng-repeat本地對象傳遞給指令控制器不起作用

Demo搗鼓什麼我想

HTML

<div ng-app="my-app" ng-controller="MainController"> 

    <div ng-repeat="document in documents"> 
     <name-row 
      document-element="document"> 
     </name-row> 
    </div> 
</div> 

指令

module.directive('nameRow', function() { 
    return { 
     restrict: 'E', 
     replace: true, 
     scope: { 
      documentElement : '=document', 

     }, 
     controller: function($scope) { 
      console.log($scope.documentElement); 
     }, 
     template: 
'  <ul>' +  
'   <li>' + 
'    <a>' + 
'     {{documentElement.targetPrice}}' + 
'    </a>' + 
'   </li>' + 
'  </ul>' 
    }; 
}); 

我只想ŧ o明白爲什麼它不起作用。我不想像鏈接等功能

回答

1

您使用的別名屬性=document訪問該任何替代做,你的屬性應該是document而不是document-element

<div ng-repeat="document in documents"> 
    <name-row 
     document="document"> 
    </name-row> 
</div> 

其他的辦法是,你可以刪除屬性的別名孤立財產申報。

scope: { 
    documentElement : '=', //just have `=` instead of `=document` 
}, 
0

更改指令。變量名是錯誤的

module.directive('nameRow', function() { 
    return { 
     restrict: 'E', 
     replace: true, 
     scope: { 
      documentElement : '=documentElement', 

     }, 
     controller: function($scope) { 
      console.log($scope.documentElement); 
     }, 
     template: 
'  <ul>' +  
'   <li>' + 
'    <a>' + 
'     {{documentElement.targetPrice}}' + 
'    </a>' + 
'   </li>' + 
'  </ul>' 
    }; 
}); 
+1

無需明確有'= documentElement'只有'='是罰款 –

+0

@PankajParkar感謝 –

相關問題