角度转换和访问控制器范围

时间:2016-03-21 19:29:31

标签: javascript html angularjs directive angularjs-ng-transclude

我有一个像这样设置的角度应用程序

<div ng-controller"FooCtrl">
  <bar></bar>
</div>

使用脚本

app.controller('FooCtrl', ['$scope', function($scope) {
  $scope.fn = function() { window.alert("Hello World"); }
}]);
app.directive('bar', function() {
  return {
    restrict: 'AE',
    templateUrl: "/bar.html",
  };
});

并且bar.html部分模板为

<button ng-click="fn();">Exec</button>

但点击该按钮不起作用。

我尝试添加

...
transclude:true
...

指令返回,但这没有成功。

如何在指令中启用对控制器功能的访问?

1 个答案:

答案 0 :(得分:2)

没有必要考虑翻译。基本上你有错字,ng-controller&amp; "FooCtrl"应以=符号

分隔
ng-controller="FooCtrl"

Plunkr Here