0

I use this fancy select for Ionic http://codepen.io/mhartington/pen/CImqy (http://jsfiddle.net/q62jx3s6/ single select!!) and I want when click on item from select to execute a function getCat from MainCtrl

 $scope.getCat = function(cat) { ..... }

who can help me ?! please ) And i also tried this example, but dosn't work for me (Angular: calling controller function inside a directive link function using &)

Community
  • 1
  • 1

3 Answers3

0

You can define a function in MainCtrl's scope and call that function whenever you click any of the options.

$scope.getCat = function() { //your code for getCat function }

So when you are configuring the options(or countries) you will need to configure it with an onclick attribute or ng-click like this :

onclick = "getCat()" or ng-click="getCat()" 

Hope it helps!

Raghvendra Kumar
  • 1,328
  • 1
  • 10
  • 26
  • I know about ng-click, but when i use them on directive this method doesn't work for me! the getCat is defined in my controller ) – NilakArgento Feb 09 '15 at 10:46
0

I tried to use $scope.$watch and works now for me. but i'll wait the correct answer

    $scope.$watch('val', function() { if($scope.val.single != null)  $scope.getCat($scope.val.single); }, true);  
0

Why don't you move 'getCat' function from controller to service and then inject service to your directive ?

Calling controller function from directive is not the best solution because then the directive is tight couple with controller.

Referring to your comment:

Maybe you could require controller in your directive, then it is injected to link function in that directive

When a directive requires a controller, it receives that controller as the fourth argument of its link function.

This topic is described here: Angular directive in section "Creating Directives that Communicate"

Markko
  • 26
  • 4