Search code examples
htmlangularjscontrollerangularjs-componentsangularjs-material

Call a controller function in a component's templateUrl


Is there a way to call a controller's function inside a component that's encapsulated inside a div, which is already run by this controller?

The component:

angular
.module('myApp')
.component('taxiCard', {
    templateUrl: '/templates/taxi.html',
    bindings: { 
        taxi: '=' ,
        cancelTaxi: '='
    },
})

The controller's function that I want to use in the template:

$scope.cancelTaxi = (taxi, id) => {
   console.log('cancelling..')
   taxi.available = true;
   taxi.history.unshift(cancel);
   dataFactory.updateTaxi(id, taxi).then(function(response){ 
   });
}

The component's templateUrl:

<md-card style="width: 300px;">
{{$ctrl.taxi._id}}
<md-button ng-show="$ctrl.taxi.available" ng-href="#!/taxies/rent/{{$ctrl.taxi._id}}">Najem</md-button> <!-- show if taxi.available -->
<md-button ng-hide="$ctrl.taxi.available" ng-click="$ctrl.cancelTaxi($ctrl.taxi, $ctrl.taxi._id)" type="submit">Preklici</md-button><!-- show if !taxi.available, torej cancel ce je rentan atm-->
<img ng-src="{{$ctrl.taxi.photo_url}}" class="md-card-image" alt="$ctrl.taxi.name" style="height:250px">
<md-card-title>
    <md-card-title-text>
    <span class="md-headline">{{$ctrl.taxi.name}}</span>
    </md-card-title-text>
</md-card-title>
<md-card-content>
    Leto izdelave: {{$ctrl.taxi.year}} <br>
    Max potnikov: {{$ctrl.taxi.max_p}} <br>
    Max hitrost: {{$ctrl.taxi.max_s}} <br>
</md-card-content>
<md-card-actions layout="row" layout-align="end center">
    <md-button ng-href="#!/taxies/details/{{$ctrl.taxi._id}}">Podrobnosti</md-button>
</md-card-actions>

Where I'm using the component:

<div class="md-padding" layout="row" ng-init="getTaxies()" layout-wrap>
<div ng-repeat="taxi in taxies">
    <a href="#!" ng-click="removeTaxi(taxi._id)">Delete</a>
    <taxi-card  taxi="taxi" 
                cancelTaxi="cancelTaxi">
    </taxi-card>   
</div>

To clarify; cancelTaxi is a function that's part of the TaxiesController, which is already run on the html site, where i use the . All the attributes in the templateUrl are displaying properly, but I don't know how to get the function to cancel the taxi on clicking the button working.

Oh yes, I did a console.log in the function and it doesn't even register it...

Thank you for your time and input!


Solution

  • This is because the component's scope is isolated. You can do two things:


    Convert your TaxiesController to a component, and require it in taxiCard.

    .component('taxiCard', {
        templateUrl: '/templates/taxi.html',
        require: {
            TaxiesController: '^TaxiesController' // ^ will look for all parents
        }
        controllerAs: 'taxiCard'
    })
    

    html:

    <div ng-click="taxiCard.TaxiesController.cancelTaxi(taxi, id)"></div>
    

    Or place all your functions from TaxiesController inside a service, and add it as a dependency to your taxiCard component:

    .component('taxiCard', {
        templateUrl: '/templates/taxi.html',
        controllerAs: 'taxiCard',
        controller: function(taxiesService) {
            this.cancelTaxi = (taxi, id) => {
                taxiesService.cancelTaxi(taxi, id);
            }
        }
    })
    

    template:

    <div ng-click="taxiCard.cancelTaxi(taxi, id)"></div>
    

    Protip: Please use the controllerAs syntax, and try to avoid $scope where possible.