1

I'm trying to call a controller's function from a component. Here are my files:

controller.js:

$scope.dataTableDevices = {
    title: 'title',
    color: {'background' : 'rgb(31, 119, 180)'},
    items: [0, 1, 2]
};
$scope.hacerClick = function(){
    alert("it works");
}

view.html:

<device-categories data="dataTableDevices"></device-categories>

deviceCategories.js:

function deviceCategoriesController() {
}

widgetsFactory.component('deviceCategories', {
    templateUrl: 'app/common/js/components/deviceCategories/deviceCategories.html',
    controller: deviceCategoriesController,
    bindings: {
        data: '='
    }
});

deviceCategories.html:

<md-button ng-click="howToCallhacerClick()">
    Click
</md-button>
jrbedard
  • 3,662
  • 5
  • 30
  • 34
Erick Ramírez
  • 75
  • 2
  • 14

2 Answers2

2

Components are like directives having an isolated scope.

If you wish to call a function which is in parent scope/ controllers scope then do the following

Consider the following method in your controller:

angular.module('MyApp').controller('AppController', function ($scope) {
    $scope.validateInputByUser = function (obj) {
        if (obj['note'].length > 255) {
            return false;
            }
            return true;
        };
});
  1. Create a component

    angular.module('MyApp')
    .component('notes', { 
                            templateUrl: "/Templates/Notes.html", 
                            controllerAs: 'model', 
                            controller: NotesController, 
                            bindings: { 
                                note: '=' 
    }});
    
  2. Create a controller with name 'NotesController' with $scope injection, as component is the child of controller, controllers 'scope' is accessible as $parent in the component.

    function NotesController ($scope) {
        // binding parent method to scope of current component
        $scope.validateInputByUser = $scope.$parent.validateInputByUser;
    };
    
  3. Now, you can implement and access the controllers method by the following:

    Html on notes template (/Templates/Notes.html) looks like

    <textarea type="text" ng-model="model.note" ng-blur="validateInputByUser(model)"/>
    

    Html on the component implementation page looks like

    <notes note="obj.notes"/>
    

Now, every time user enters text and leaves the text area the controller's function 'validateInputByUser' will be called.

Hope this helps! Cheers...

0

You need to pass the function to your component from your controller using the '&' binding which is used for callbacks to component events. So You'll need to do something like this:

component

.component('deviceCategories',{
    template: `<div>
                   <md-button ng-click="$ctrl.hacerClickFn()">Click Me!
                   </md-button>
               </div>,
    bindings: {
        data:'=', //assuming you need two way binding
        hacerFunc:'&'
     },
     controller: [function(){
         var ctrl = this;

          ctrl.hacerClickFn = function() {
              ctrl.hacerFunc();
          }
     }]
 });

View

<device-categories data="data" hacer-func="hacerClick()"</device-categories>

AngularJS Component Documentation

Great explanation of differences between components and directives

Justin Kruse
  • 1,020
  • 12
  • 27