Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
90 views
in Technique[技术] by (71.8m points)

javascript - How to share a variable between different parts of a JS directive?

I'm trying to share data between the link and controller part of a directive with the overall goal being to measure the amount of time it takes for a div to change state / the data of the page to load.

angular.module("myApp")
.directive("loadtime", [
  function(){
      return {
          restrict: 'A',
          link: function (scope, element, attrs) {
              scope.$watch('dataLoaded', function(newValue, oldValue){
                  if(newValue === true) {
                      var endTime = new Date().getTime();
                      console.log(`data finished loading for ${attrs.id} on ${endTime}!`);
                      console.log(`total time to load (in ms): ${(element.startTime - endTime) / 1000}`);
                  }
              });
          },
          controller: function($scope, $element, $attrs, $injector) {
              var startTime = new Date().getTime();
              console.log(`Loading has started on ${startTime}`);
          }
      };
  }
]);

What's the best way to set variables within the directive to then have them accessible throughout the different sections/parts of the directive?

question from:https://stackoverflow.com/questions/65713745/how-to-share-a-variable-between-different-parts-of-a-js-directive

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This may or may not be a good approach but I was able to share variables between link and controller the following way:

angular.module("myApp")
.directive("loadtime", [
function(){

    let startTime;
    function setStartTime(){
        if(!startTime){
            startTime = new Date().getTime();
        }
    }

    let endTime;
    function setEndTime(){
        if(!endTime){
            endTime = new Date().getTime();
        }
    }
    return {
        restrict: 'AE',
        link: function (scope, element, attrs) {
            scope.$watch('dataLoaded', function(newValue, oldValue){
                if(newValue === true) {
                    setEndTime();
                    console.log(`data finished loading for ${attrs.id} at ${endTime}!`);
                    console.log(`total time to load (in ms): ${(endTime - startTime)}`);
                }
            });
        },
        controller: function($scope, $element, $attrs) {
            setStartTime();
            console.log(`Loading has started on ${startTime}`);
        }
    };
}
]);

Essentially just declare variables in the outermost function of the directive and set their values using a function.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...