javascript - angular directive call to controller method with parameter -


i have controller method gets parameter, , want directive call method , pass parameter.

how add tag controller when user clicks tag inside directive.

a bit of code:

app.controller('mainctrl', function($scope) {      $scope.chosentags = ['clicktoremoveme', 'if click on tags below should appear here'];      $scope.jokes = [{         id: 0,         content: 'bla bla bla bla',         tags: ['fat', 'blondes']     },{         id: 1,         content: 'another another ',         tags: ['thin', 'dark']     }];      $scope.addtag = function(tag) {         $scope.chosentags.push(tag);     },      $scope.removetag = function(tag) {         removea($scope.chosentags, tag);     }  });  app.directive("joke", [function () {      return {         restrict: 'e',         templateurl: 'joke.html',         scope: {             joke: '='         }     }; }]); 

plunker: http://plnkr.co/edit/vpip4mwmweh8lvi7i5t5?p=preview

you're using isolated scope, the ng-click="addme(tag)" won't anything, since addme function not exist anywhere on scope.

you have several options, added function reference , put in scope:

directive:

scope: {     joke: '=',     addme: '=' } 

html:

<joke ng-repeat="joke in jokes" joke="joke" add-me="addtag"></joke> 

plunker

also should add check before inserting chosentags array avoid console errors:

$scope.addtag = function(tag) {     if ($scope.chosentags.indexof(tag) === -1) {         $scope.chosentags.push(tag);     } } 

Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -