我想不通爲什麼this plnkr不起作用。函數(表達式)綁定不起作用
我試圖將一個簡單的函數從父控制器綁定到子自定義指令。如果我使用&
,它不起作用,但如果我使用=
或<
,它工作正常。我明白這是不好的做法,但爲什麼它能正常工作並且&
不?也許我錯過了很簡單的事情?
這裏的腳本:
JS
var app = angular.module('myApp',['ngMaterial']);
app.controller('mainCtrl',mainCtrl);
function mainCtrl(){
var main = this;
main.test = function(){console.log("test")};
}
app.directive('myDirective',myDirective);
function myDirective(){
return {
scope: {},
controller: myCtrl,
controllerAs: "dir",
bindToController: {
//fn: '&' //This doesn't work
fn: '<' // This works
},
template: '<md-button ng-click="dir.fn()" class="md-raised">click</md-button>'
};
function myCtrl(){
}
}
HTML
<div ng-app="myApp" ng-controller="mainCtrl as main">
<my-directive fn="main.test"></my-directive>
</div>
我希望這有助於http://stackoverflow.com/questions/21712147/angularjs-differences-among-in-directive-scope –