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>
2
  • check stackoverflow.com/questions/18378520/… Commented Sep 23, 2016 at 12:23
  • I have not been able to solve the problem. Components behave a little bit different than directives, right? Commented Sep 23, 2016 at 22:37

2 Answers 2

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...

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.