I'm trying to write a directive that will generate a grid. The following code works but I have to specify the controller name 'DemoCtrl'. Is it possible to retrieve the current controller name from within the directive so I can pass it through to the buildColumn/buildRows functions?
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;
}
}
In AngularJS, a Controller is defined by a JavaScript constructor function that is used to augment the AngularJS Scope. Controllers can be attached to the DOM in different ways.
The $scope in an AngularJS is a built-in object, which contains application data and methods. You can create properties to a $scope object inside a controller function and assign a value or function to it. The $scope is glue between a controller and view (HTML).
The ngInit directive allows you to evaluate an expression in the current scope. This directive can be abused to add unnecessary amounts of logic into your templates. There are only a few appropriate uses of ngInit : aliasing special properties of ngRepeat , as seen in the demo below.
ng-controller directive tells AngularJS what controller to use with this view. AngularJS application mainly relies on controllers to control the flow of data in the application.
You could use your route service to get the controller name
{{$route.current.scope.name}}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With