0

I have a directive called orders-list and i need to bind the object currentOrder inside the controller with object inside the loop that was clicked on;

//controler
app.controller("receivedOrders", ['$scope', 'orders', 'Order', 'currentOrder', function ($scope, orders, Order, currentOrder) {
  $scope.currentOrder = null;
  $scope.orders = orders['content'];


//directive
app.directive("ordersList", function () {
  return {
    restrict: 'E',
    templateUrl: 'templates/orders_list.html',
    scope: {orders: '=', currentOrder: '='},
    controller: ['$scope','$attrs', function ($scope, $attrs) {
        $scope.selectOrder =function(order){
            $attrs.currentOrder=order;
        };
     //some other functions
    }]
}

//directive definition
<orders-list current-order="currentOrder" orders="orders"></orders-list>

//directive body
<md-list>
    <order ng-repeat="order in orders|filter:query|orderBy:activeSorting.directionSign+activeSorting.name"
           order="order" ng-click="selectOrder(order)"></order>
</md-list>

-------- Changes---------

So ok now i pass the function selectOrder to the inner directive order, and invoke the selectOrder function from it`s scope, but it still does not work =(

app.directive('order', function () {
return {
    restrict: "E",
    templateUrl: '/templates/order.html',
    scope: {
        order: '=',
        orderClick:'&'
    },

<md-list-item order-click="orderClick({order:order})" class="md-3-line">

<order ng-repeat="order in orders|filter:query|orderBy:activeSorting.directionSign+activeSorting.name"
           order="order" order-click="selectOrder(order)"></order>
3
  • 1
    Should that be $scope.currentOrder=order; Commented Aug 24, 2016 at 16:07
  • @Developer Yeees thanks this have worked, i am a blind idiot Commented Aug 24, 2016 at 16:24
  • lol, happens to everyone. Sometimes all you need is extra pair of eyes to fix issues Commented Aug 24, 2016 at 16:42

2 Answers 2

1

Change:

$attrs.currentOrder=order;

to:

$scope.currentOrder=order;
Sign up to request clarification or add additional context in comments.

Comments

1

It is because ng-repeat creates a new scope for each element in orders property.

The ngRepeat directive instantiates a template once per item from a collection. Each template instance gets its own scope, where the given loop variable is set to the current collection item, and $index is set to the item index or key.

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.