javascript - AngularJS : Passing a function to an isolated scope of a directive to be called within its controller? -
i'm trying call function passed controller's scope directive via "&" operation directive's controller. method, however, claimed angular undefined. after reading code on , over, scouring internet, , repeating process, i've decided turn here.
here's relevant part of controller. contains method pass directive.
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; }); }; }]);
here's directive. unable invoke onpost.
angular.module('myapp.directives', []).directive('compose', ['postalservice', function(postalservice) { return { restrict: 'e', transclude: false, replace: true, scope: { onpost: "&" //why 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 not invoke onpost()?? $scope.onpost(); } }); }; }, }; }]);
and here's relevant part of html
<div ng-controller="postctrl"> <section class="side-bar panel hide-for-small"> <compose onpost="getposts()"></compose> </section> <!--more, non-relevant html here--> </div>
i know problem not postalservice service. instead, directive reports no function passed it. why??
replace
<compose onpost="getposts()"></compose>
with
<compose on-post="getposts()"></compose>
and it'll work.
the angular docs why it's so:
directives have camel cased names such ngbind. directive can invoked translating camel case name snake case these special characters :, -, or _.
Comments
Post a Comment