1
如何讓我的指令調用父母的控制器功能?我有一個指令,它看起來像這樣:角度指令調用父控制器功能
HTML:
<div ng-app="myapp" ng-controller="mainController as mainCtrl">
some body text<br>
<a href="javascript:;" ng-click="mainCtrl.myfunc()">Click Me from body</a>
<photo photo-src="abc.jpg" caption="This is so Cool" />
</div>
的Javascript:
var app = angular.module('myapp',[]);
app.controller('mainController', function(){
var vm = this;
vm.myfunc = myfunc;
function myfunc(){
console.log("Log from myfunc");
}
});
app.directive('photo',function(){
return {
restrict: 'E',
template: '<figure> <img ng-src="{{photoSrc}}" width="500px"> <figcaption>{{caption}}</figcaption> <a href="javascript:;" ng-click="mainCtrl.myfunc()">Click Me</a></figure>',
replace: true,
scope: {
caption: '@',
photoSrc: '@'
}
}
});
當我點擊Click Me from body
鏈接,它顯示控制檯日誌消息預期。但是,當我點擊Click Me from directive
時,什麼也沒有發生。我怎樣才能使它工作?
使用'&'將函數傳遞到'scope',就像用'@'傳遞其他參數一樣。 – Claies
檢查此videotutorial關於隔離示波器。它包含您需要的實際示例,它可能對您有用https://egghead.io/lessons/angularjs-understanding-isolate-scope – Mindastic