[英]Customer Directive ng-repeat scope not getting in inside function Angular JS?
我需要獲取自定義指令范圍值。 我正在嘗試訪問函數內部的scope.cellId。 它不起作用。 能否請任何人為此提供解決方案。
提前致謝..
這里的小提琴鏈接:
這是一些控制器:
var bosAppModule = angular.module('testapp', []);
bosAppModule.controller('testCtrlr',['$scope', function($scope){
$scope.layoutData =[ {layouttablecelltableid:"dsafdasfasdf"},{layouttablecelltableid:"aaaaaa"}]
}]);
bosAppModule.directive('layoutTableCellView',function($compile){
var layoutTableCellObj={};
linkFnTableCell=function(scope, element, attributes, controllerCtrl) {
console.log("#####"+scope.cellId);
};
layoutTableCellObj.restrict='AE';
layoutTableCellObj.replace='true';
layoutTableCellObj.scope={layoutData:'=',cellId:'@'};
layoutTableCellObj.template="<div cell-id='tablecell.layouttablecelltableid' style='background-color:grey;height:200px;' layout-data='layoutData' ng-repeat='tablecell in layoutData' >{{tablecell.layouttablecelltableid}}</div>";
layoutTableCellObj.link = linkFnTableCell;
return layoutTableCellObj;
});
這是HTML:
<div ng-app="testapp">
<div ng-controller="testCtrlr">
<layout-table-cell-view> </layout-table-cell-view>
</div>
</div>
我不確定我是否了解您要達到的目標。 您能提供更多細節嗎? 我已經清理了你的提琴,因為這有點亂,我不喜歡用角形:
https://jsfiddle.net/6xLt4pja/1/
指令:
bosAppModule.directive('layoutTableCellView', [
function () {
return {
restrict: 'AE',
template: "<div style='background-color:grey;height:200px;' ng-repeat='tablecell in layoutData'>{{tablecell.layouttablecelltableid}}</div>",
scope: {
layoutData:'='
},
link: function (scope) {
console.log('SCOPE: ', scope.layoutData);
}
};
}
]);
和html:
<div ng-app="testapp">
<div ng-controller="testCtrlr">
<layout-table-cell-view layout-data="layoutData"></layout-table-cell-view>
</div>
</div>
不完全確定要做什么,但是如果要訪問已經聲明的作用域值,則可以在函數中像這樣訪問它們:
linkFnTableCell = function(scope, element, attributes, controllerCtrl) {
console.log("#####" + scope.cellId); // your own log
for(var i = 0; i < scope.layoutData.length; i++){
console.log("#####" + scope.layoutData[i].layouttablecelltableid);
}
};
這將為您提供所有內部ID-如果您還需要cellId,則語法將變得更加復雜
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.