[英]How to pass value from directive to template?
I have external template. 我有外部模板。 I need to create custom directive which will have attribute/parameter/value/whatever which will be passed to template upon it's render. 我需要创建自定义指令,该指令将具有属性/参数/值/将在呈现时传递给模板的任何内容。 How to do it? 怎么做?
It should be easy like this: 像这样应该很容易:
HTML 的HTML
<table>
<cell rownum="0"></cell>
<cell rownum="1"></cell>
<cell rownum="2"></cell>
<cell rownum="3"></cell>
</table>
AngularJS AngularJS
prototypeApplication.directive('cell', function() {
return {
restrict: 'E',
replace: true,
transclude: true,
scope: { rownum:'@' },
templateUrl: 'views/prototype/booking/templates/table-row.html'
};
});
Template: 模板:
<tr>
<td>
<div class="form-group first-name">
<label>First name</label>
<input type="text" ng-class="{'input-valid': isValidField('FirstName', persons[{{rownum}}].firstName)}"
name="firstName" class="form-control input-name" ng-model="persons[{{rownum}}].firstName"
ng-focus="focused('inputFirstName', {{rownum}})" placeholder="-">
</div>
</td>
</tr>
At the moment I am getting : 目前,我得到:
Error: [$parse:syntax] http://errors.angularjs.org/1.3.3/$parse/syntax?p0=%7B&p1=invalid%20key&p2=28&p3=focused('inputFirstName'%2C%20%7B%7Brownum%7D%7D)&p4=%7Brownum%7D%7D)
UPDATE 更新
When I changed {{rownum}}
to rownum
it solved problem but new one appeared: 当我将{{rownum}}
更改为rownum
它解决了问题,但出现了新的问题:
It seems that it cannot access general scope so ng-model
is no longer works. 似乎它无法访问常规范围,因此ng-model
不再起作用。 How to fix it? 如何解决?
If I remember correctly if you specify a scope
for your directive you create an isolated-scope which means that you can no longer access the parent scope. 如果我没有记错,如果您为指令指定了scope
,则会创建一个隔离范围,这意味着您将无法再访问父作用域。 You could get the rownum like this without creating an isolated-scope: 您可以像这样获得rownum,而无需创建孤立范围:
prototypeApplication.directive('cell', function() {
return {
restrict: 'E',
replace: true,
transclude: true,
templateUrl: 'views/prototype/booking/templates/table-row.html',
link: function(scope, element, attrs) {
scope.rownum = scope.$eval(attrs.rownum);
}
};
}); });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.