我的HTML看起來像這樣:如何在角度1.x中調用兩次嵌套方法?
控制器HTML:
<div ng-controller="Ctrl">
<first-directive></first-directive>
</div>
第一指令HTML
<li>
<second-directive></second-directive>
</li>
控制器JS:
app.controller('Ctrl', (#scope) => {
$scope.foo = function() {
console.log('do smthn');
}
});
第一指令:
app.directive('first-directive', function(){
return {
restrict: 'E',
templateUrl: '/partials/first-directive.html',
replace: true,
scope: {
// some data
}
controllerAs: function(){}
}
}
第二指令:
app.directive('second-directive', function(){
return {
restrict: 'E',
templateUrl: '/partials/second-directive.html',
controllerAs: function(){}
}
}
所以我有嵌套的指令控制器和有另一個嵌套指令。當我試圖從第一個指令調用$ parent.foo()時,它可以工作。當我試圖從第二指令調用$ parent。$ parent.foo()時,它不起作用。我也嘗試使用第二指令語法中的ng-controller="Ctrl as ctrl"
和ctrl.foo()
,但它也不起作用。爲什麼?
我沒有在ng-controller中添加第二個指令。我添加second-directive的唯一地方是在first-directive中。我不使用ng,如果有的話。 –
你可以嘗試從第二個指令$ parent.foo(),因爲它沒有隔離範圍。 –
我不認爲這是兩層嵌套 –