0

I have a list of itineraries displayed on a page. Each itinerary has a select button. This select button is a directive.

.directive('itinerarySelectBtn', ['itineraryFactory', 
    function(itineraryFactory){
        return {
            restrict: 'E', // defining the container as an element
            scope: {
                itinerary: '=', // html element we are injecting this directive too
            },
            replace: true,
            templateUrl: '/content/partials/directives/results/results-list.html',
            link: function(scope, element, attrs){
                element.on('click' , function(){
                    itineraryFactory.itinerary = scope.itinerary;
                });
            }
        }
    }
])

This button does nothing else, other than receive the itinerary object via scope: '=' and then assigns it in my itineraryFactory to the itinerary object. I do this so I can utilise my getter and setter in my itineraryFactory and pull it into my controller. In my controller:

$scope.itineraries = itineraryFactory.getItinerary();

My question is: Is this overkill, should this be a directive? Alternatively I could just have a $scope.function in my controller and do the same thing. The button could just be part of the ng-repeat that lists the itineraries on the page.

I feel like having a setItinerary() function in my controller would essentially be the same and could be executed via ng-click on the button.

Thoughts?

Regards,

2
  • 1
    I can't see you needing anything beyond <button ng-click="controllerScopeItinerary = repeaterItinerary">Click me!</button> Commented Oct 28, 2015 at 2:36
  • Actually that is even better. You are tight. I have over-complicated things. Feel free to post that as an answer if you'd like. Many thanks! Commented Oct 28, 2015 at 2:38

1 Answer 1

1

You can do everything in your template.

Say you want to assign $scope.chosenItinerary in your controller from the list in $scope.itineraries

<div ng-repeat="itinerary in itineraries">
    <button ng-click="chosenItinerary = itinerary">Pick this one!</button>
</div>
Sign up to request clarification or add additional context in comments.

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.