javascript 在 AngularJS 中获取当前控制器名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30962192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get Current Controller Name in AngularJS
提问by Foxy
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?
我正在尝试编写一个将生成网格的指令。以下代码有效,但我必须指定控制器名称“DemoCtrl”。是否可以从指令中检索当前控制器名称,以便我可以将其传递给 buildColumn/buildRows 函数?
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;
}
}
回答by Siddharth Pandey
You could use your route service to get the controller name
您可以使用路由服务来获取控制器名称
{{$route.current.scope.name}}
回答by Foxy
I've updated my code with my solution, I decided to create a new scope variable with the controller name which gets passed through to the buildGrid function. Not ideal, but it works!
我已经用我的解决方案更新了我的代码,我决定使用控制器名称创建一个新的范围变量,该变量将传递给 buildGrid 函数。不理想,但它有效!