में वर्तमान नियंत्रक का नाम प्राप्त करें मैं एक निर्देश लिखने की कोशिश कर रहा हूं जो ग्रिड उत्पन्न करेगा। निम्न कोड काम करता है लेकिन मुझे नियंत्रक नाम 'DemoCtrl' निर्दिष्ट करना होगा। क्या वर्तमान नियंत्रक नाम को निर्देश के भीतर से पुनर्प्राप्त करना संभव है ताकि मैं इसे बिल्ड कॉलम/buildRows फ़ंक्शंस के माध्यम से पास कर सकूं?AngularJS
angular.module('app').controller('DemoCtrl', function ($scope) {
$scope.controller = "DemoCtrl";
$scope.coldata = [
{name: 'Account'},
{name: 'Name'}
];
$scope.rowdata = [
{
"account": "ABC",
"name": "Jim",
},
{
"account": "DEF",
"name": "Joe",
},
{
"account": "GHI",
"name": "Fred",
}
];
});
angular.module('foxy.components.grid', [])
.controller('GridController', ['$scope', '$attrs', function ($scope, $attrs) {
}])
.directive('grid', function ($compile) {
return {
restrict: 'EA',
controller: 'GridController',
require: "^ngController",
scope: {
data: "=",
columns: "=",
controller: "="
},
link: function (scope, element, attrs, ctrl) {
scope.$watch('data', function() {
var el = $compile(buildGrid(scope.controller))(scope);
element.replaceWith(el);
element = el;
});
}
};
})
function buildGrid(controller) {
var html = "<table>";
html += "<thead>";
html += buildColumn(controller);
html += "</thead>";
html += "<tbody>";
html += buildRows(controller);
html +="</body>";
html += "</table>";
return html;
}
function buildColumn(controller) {
try {
var html = "";
var dom_el = document.querySelector('[ng-controller="' + controller + '"]');
var ng_el = angular.element(dom_el);
var ng_el_scope = ng_el.scope();
var colname = ng_el_scope.coldata;
html += "<tr>";
for (i = 0; i < colname.length; i++) {
html += "<th>";
html += colname[i]["name"];
html += "</th>";
}
html += "</tr>";
return html;
} catch (err) {
return "#error" + err;
}
}
function buildRows(controller) {
try {
var html = "";
var dom_el = document.querySelector('[ng-controller="' + controller + '"]');
var ng_el = angular.element(dom_el);
var ng_el_scope = ng_el.scope();
var colname = ng_el_scope.coldata;
var rows = ng_el_scope.rowdata;
for (j = 0; j < rows.length; j++) {
html += "<tr>";
for (data in rows[j]) {
html += "<td>";
html += rows[j][data];
html += "</td>";
}
html += "</tr>";
}
return html;
} catch (err) {
return "#error" + err;
}
}
आपकी समस्या से पूरी तरह से असंबंधित है, लेकिन आपको '+ =' ऑपरेटर चेकआउट करना चाहिए। – m90