apn*_*ing 5 angularjs angularjs-directive
我想在transcluded部分中使用指令,转换内容和调用指令的控制器方法:
<mydirective>
<div ng-click='foo()'>
click me
</div>
</mydirective>
app.directive "mydirective", ->
return {
restrict: 'EACM',
transclude: true
template: "<div ng-transclude></div>"
scope: { } #required: I use two way binding on some variable, but it's not the question here
controller: [ '$scope', ($scope)->
$scope.foo = -> console.log('foo')
]
}
Run Code Online (Sandbox Code Playgroud)
我该怎么办?
我有一个不同的答案,这不是一个黑客,我希望它会被接受..
看我的plunkr进行现场演示
<div custom-directive custom-name="{{name}}">
if transclude works fine you should see my name right here.. [{{customName}}]
</div>
Run Code Online (Sandbox Code Playgroud)
注意我customName在指令中使用,并为其指定一个值作为指令范围的一部分.
angular.module('guy').directive('customDirective', function($compile, $timeout){
return {
template : '<div class="custom-template">This is custom template with [{{customName}}]. below should be appended content with binding to isolated scope using the transclude function.. wait 2 seconds to see that binding works</div>',
restrict: 'AC',
transclude: true,
scope : {
customName : '@'
},
link : function postLink( scope, element, attrs, dummy, transcludeFn ){
transcludeFn( scope, function(clone, innerScope ){
var compiled = $compile(clone)(scope);
element.append(compiled);
});
$timeout( function(){
scope.customName = 'this stuff works!!!';
}, 2000);
}
}
});
Run Code Online (Sandbox Code Playgroud)
请注意,我将在2秒后更改范围上的值,以便显示绑定工作.
在网上阅读了很多内容后,我理解了以下内容:
$digest| 归档时间: |
|
| 查看次数: |
3616 次 |
| 最近记录: |