1

I have this Directive:

.directive('reorderDiv', function ($compile) {
    return function (scope, elem, attrs) {

        function shuffle(array) {
            var currentIndex = array.length, temporaryValue, randomIndex;

            // Elemente
            while (0 !== currentIndex) {

                // Die restlichen Elemente
                randomIndex = Math.floor(Math.random() * currentIndex);
                currentIndex -= 1;

                temporaryValue = array[currentIndex];
                array[currentIndex] = array[randomIndex];
                array[randomIndex] = temporaryValue;
            }

            return array;
        }
        elem.on('click', function () {

          console.log('called');
          var divs = angular.element(document.querySelector('.center'));
          console.log(divs);

          divs = shuffle(divs);
          var content = $compile(divs)(scope);
          elem.append(content);
        })
    }
})

My goal is, that if the view loads it should run the function shuffle and not on a click on the element. How can i do this?

I tried to use elem.on("load")[...] but this didn't work. Could somebody explain me, why this does not work?

3
  • Just call shuffle function after on click event.. Commented Jul 27, 2016 at 16:47
  • hmm, that did not work. Also after a refresh.. Commented Jul 27, 2016 at 16:48
  • Check this blog.thoughtram.io/angularjs/2016/03/29/… Commented Jul 27, 2016 at 17:21

2 Answers 2

1

You can use $onInit event to fire shuffle function when directive loads:

.directive('reorderDiv', function($compile) {
  function shuffle(array) {
    var currentIndex = array.length, temporaryValue, randomIndex;

        // Elemente
        while (0 !== currentIndex) {

            // Die restlichen Elemente
            randomIndex = Math.floor(Math.random() * currentIndex);
            currentIndex -= 1;

            temporaryValue = array[currentIndex];
            array[currentIndex] = array[randomIndex];
            array[randomIndex] = temporaryValue;
        }

        return array;
  }
  var obj = {
    controller: function() {
      this.$onInit = function() {
        var divs = angular.element(document.querySelector('.center'));
        console.log(divs);

        shuffle(divs);
      }
    },
    link: function(scope, elem, attrs) {

      elem.on('click', function() {

         console.log('called');

         var divs = angular.element(document.querySelector('.center'));
         console.log(divs);

         divs = shuffle(divs);
         var content = $compile(divs)(scope);
         elem.append(content);
      })
    }

  };
  return obj;
})
Sign up to request clarification or add additional context in comments.

Comments

0

Just call the shuffle function with correct parameter inside directive. It will run.

3 Comments

That didn't work. I removed the on.click. Logs get called but the divs won't reorder.
have you called like divs = shuffle(divs); or shuffle(divs); ?
Like so: divs = shuffle(divs); var content = $compile(divs)(scope); elem.append(content); When i call it with shuffle(divs); also nothing happens. Is there a way to fire it when the view loads?

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.