我正在尝试通过指令控制器的“&”操作将从控制器范围传递的函数调用到指令中。然而,Angular 声称该方法是未定义的。在一遍又一遍地阅读我的代码,搜索互联网,然后重复这个过程之后,我决定在这里寻求帮助。
这是我的控制器的相关部分。它包含我传递给我的指令的方法。
angular.module('myApp.controllers', []).controller('PostCtrl', ['$scope', 'postalService', function($scope, postalService) {
$scope.posts = [];
$scope.getPosts = function() {
postalService.getPosts(function(err, posts) {
if(err);
else $scope.posts = posts;
});
};
}]);
这是我的指示。我无法调用 onPost。
angular.module('myApp.directives', []).directive('compose', ['postalService', function(postalService) {
return {
restrict: 'E',
transclude: false,
replace: true,
scope: {
onPost: "&" //why will it not
},
templateUrl: "partials/components/compose-partial.html",
controller: function($scope, postalService) {
$scope.title = "";
$scope.content = "";
$scope.newPost = function() {
postalService.newPost($scope.title, $scope.content, function(err) {
if(err) console.log(err + ":(");
else {
console.log("Success getting posts.");
//why can I not invoke onPost()??
$scope.onPost();
}
});
};
},
};
}]);
这是我的html的相关部分
<div ng-controller="PostCtrl">
<section class="side-bar panel hide-for-small">
<compose onPost="getPosts()"></compose>
</section>
<!--more, non-relevant html here-->
</div>
我知道问题不在于我的 postalService 服务。相反,该指令报告没有函数传递给它。为什么??