$ctrl对象在transcluded作用域中不可用
$ctrl object not available in transcluded scope?
使用ng-transclude
时,我无法访问新angular.component()
的$ctrl
作用域绑定。
我想访问$ctrl
而不是页面上的.controller()
,因为我们希望迁移到angular2而不是轨道。
这里有一个演示者:https://plnkr.co/edit/M4k7Av8Q2OrMSsvhZjsb?p=preview
希望有人能给我们一些启示!以下代码:
// app.js
angular.module('test.app', [])
.component('transcludedComponent', {
transclude: true,
template: '<div ng-transclude></div>',
controller: function(){
this.text = 'hello world'
}
})
.component('regularComponent', {
template: '<h2>{{$ctrl.otherText}}</h2>',
controller: function(){
this.otherText = 'this binding works';
}
});
angular.element(document.body).ready(function(){
angular.bootstrap(document, ['test.app']);
});
<!-- index.html -->
<transcluded-component>
<h2>transcluded text has no $ctrl: {{ $ctrl.text }}</h2>
<regular-component></regular-component>
</transcluded-component>
任何有同样问题的人,我只是用$parent
代替$ctrl
来访问正确的作用域。虽然很糟糕,但它很管用。
相关文章:
- 监视函数从服务返回不起作用,但作用域函数起作用
- 将作用域存储在JSON中
- 从控制器继承了隔离的作用域以生成可重用的指令
- 从ng模板访问作用域
- 调用私有作用域
- 对父作用域的指令更新延迟了一步
- 正在$rootScope上达到控制器作用域$在内部控制器上
- 两个指令创建新的继承的和隔离的作用域-元素得到哪个
- 这两个关于 JavaScript 作用域链的例子有什么区别?
- 在具有命名作用域的自定义Polymer元素中隐藏表行
- AngularJS指令隔离作用域
- Angularjs:修改js中的作用域,稍后在页面中使用
- 访问多个指令的隔离作用域
- Javascript作用域和Ajax调用;工作不正常
- 向Angular作用域对象添加对象数组——TypeError
- ng重复中的ng模型-初始化唯一作用域属性
- 在put方法之前从作用域获取数据
- 如何在html中以角度显示自定义指令的作用域
- 当提供函数名称时,检查函数是否存在于同一作用域中
- $ctrl对象在transcluded作用域中不可用