Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Current Controller Name in AngularJS

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;
    }
}
like image 532
Foxy Avatar asked Jun 21 '15 07:06

Foxy


People also ask

What are the controllers in AngularJS?

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.

What is $scope in AngularJS?

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).

What is ngInit?

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.

Which is the valid statement of controller in AngularJS?

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.


1 Answers

You could use your route service to get the controller name

{{$route.current.scope.name}}
like image 139
Siddharth Pandey Avatar answered Sep 18 '22 18:09

Siddharth Pandey