[英]can't pass dynamic parameter to angular directive
I just took over the frontend work of our angularjs apps and I'm stuck.我刚刚接手了我们的 angularjs 应用程序的前端工作,但我被卡住了。
I've been creating directives to replace bloated html to make updating the frontend look easier.我一直在创建指令来替换臃肿的 html,以使更新前端看起来更容易。 All was going well till I hit the voting page of our elections app.
一切都很顺利,直到我点击了我们的选举应用程序的投票页面。
directive passing param (none work)指令传递参数(无效)
<div block-container header="vm.electionToVote.name" startrow="'false'">
<div block-container header="'vm.electionToVote.name'" startrow="'false'">
<div block-container header="{{vm.electionToVote.name}}" startrow="'false'">
usually these work通常这些工作
<div block-container header="'Elections List'">
<div block-container header="vm.full.title" startrow="'false'">
directive html <h3>{{style.header}}</h3>
指令 html
<h3>{{style.header}}</h3>
directive指示
.directive('blockContainer', blockContainer);
/* @ngInject */
function blockContainer() {
var directive = {
scope: {
header: '=',
startrow: '='
},
replace: true,
transclude: true,
controller: blockContainerCtrl,
controllerAs: 'style',
templateUrl: 'app/style/directives/blockContainer.tpl.html',
restrict: 'A'
};
return directive;
function blockContainerCtrl($scope) {
//debugger;
var vm = this;
vm.header = $scope.header;
vm.startrow = angular.isDefined($scope.startrow) ? $scope.startrow : 'true';
}
}
running debug shows that vm.electionToVote is undefined but ng-inspector shows that is has stuff (id, name, endDate etc) screenshot: http://i.imgur.com/d6nbAsV.png运行调试显示 vm.electionToVote 未定义,但 ng-inspector 显示有内容(id、名称、结束日期等)截图: http : //i.imgur.com/d6nbAsV.png
you can see all (including election) files here: https://plnkr.co/edit/bPVp8QY0xzlJ6aWZoRDi?p=preview您可以在此处查看所有(包括选举)文件: https : //plnkr.co/edit/bPVp8QY0xzlJ6aWZoRDi?p=preview
I'm really an angualjs beginner, but with google, stackoverflow and a lot of trial and error, I'm getting the job done... sort of...我真的是一个 angualjs 初学者,但是通过 google、stackoverflow 和大量的反复试验,我完成了工作......有点......
any other tips/advice would be greatly appreciated as well任何其他提示/建议也将不胜感激
As you have used style.header
on HTML to bind header
value on HTML, you should add bindToController: true
in your directive so that all the isolated scope bindings will be available inside your directive html.由于您已在 HTML 上使用
style.header
绑定 HTML 上的header
值,因此您应该在指令中添加bindToController: true
以便所有隔离范围绑定都将在您的指令 html 中可用。
Directive指示
var directive = {
scope: {
header: '=',
startrow : '='
},
replace: true,
transclude: true,
controller: blockContainerCtrl,
controllerAs: 'style',
templateUrl: 'app/style/directives/blockContainer.tpl.html',
restrict: 'A',
bindToController: true //<-- Added this line
};
Directive Usage指令用法
<div block-container header="vm.electionToVote.name" startrow="'false'">
Additionally you should not be doing header
& startrow
variable assignment inside controller manually.此外,您不应手动在控制器内部进行
header
和startrow
变量分配。 Removing those two assignment part would make it work.删除这两个分配部分将使其工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.