0

I am updating a scope array called $scope.arrangement using a click function, but this is not causing my directive to update and update the DOM.

/******************************************
  Sort function
******************************************/  

  $scope.sort = function() {
        $scope.arrangement.sort(function(a, b){
            var keyA = a.created,
        keyB = b.created
        // Compare the 2 dates
        if(keyA < keyB) return -1;
        if(keyA > keyB) return 1;
        return 0;
        })
        for (b=0;b<$scope.arrangement.length;b++) { 
            if($scope.arrangement[b].ord !== b) {
                console.log('order was '+$scope.arrangement[b].ord + " now " + b)
                $scope.arrangement[b].ord=b;
            }
        }
    }
})

/******************************************
  Directives
******************************************/

app.directive('item', function() {
  return {
    restrict: 'A',
    scope: {
      count: "=",
      arrangement: "="
    },
    link: function(scope,element,attrs) {
      console.log('updated')
      for (a=0;a<scope.arrangement.length;a++) {
        if (scope.arrangement[a].ord === scope.count) {
          element.html("Item "+scope.arrangement[a].id + " Created: " + scope.arrangement[a].created)
        }
      }
    }
  }
})

Here's a plunker: http://plnkr.co/edit/hT0HQ85yaPkQj2JcASrY

By clicking 'sort', I am expecting the $scope.sort function to run and this in turn to refresh my directive. What actually happens is the $scope.sort function runs but the directive does not update.

Note that I deliberately not using an ng-repeat here.

2
  • You seem to have the arrangement and counts set up as 2 way scope bound properties. However, you specify them as attributes. did you mean to use the '@' scope binding? Commented Aug 27, 2014 at 14:44
  • I thought that by binding them they would update the parent scope and therefore trigger the directive to update? Could you show me what you would change in plunker please? Commented Aug 27, 2014 at 14:59

1 Answer 1

2

The link function only runs one time. elem.html does not use data bindings, so this will just set up the HTML of the element initially when the directive is linked. You can set up a watch yourself:

scope.$watchCollection('arrangement', function () {
  for (a=0;a<scope.arrangement.length;a++) {
    if (scope.arrangement[a].ord === scope.count) {
      element.html("Item "+scope.arrangement[a].id + " Created: " + scope.arrangement[a].created)
    }
  }
});

Seems like it would be much easier to use built in directives, though:

<span ng-if="arr.ord == s.count" ng-repeat="arr in arrangement">
  Item {{arr.id}} Created: {{arr.created}}
</span>
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.