如何在指令中使用$ timeout服务?

时间:2022-09-18 20:36:12

Basically, I want to measure the width of the element after angular has manipulated the DOM. So I would like to use $timeout for that, but it keeps getting me errors.

基本上,我想在角度操纵DOM之后测量元素的宽度。所以我想使用$ timeout,但它一直让我犯错误。

HTML

   <div ng-app="github">
      <ul mynav>
        <li ng-repeat="nav in navItems">{{nav.name}}</li>
      </ul>

      </div>
    </div>

CSS

ul,li {
  display:inline-block;
}
li {
  margin-right:1em;
}

JS

(function() {
  angular.module('github', [])
    .directive('mynav', function($window) {
      return {
        restrict: 'A',
        link: function(scope, element, attrs, timeout) {
          scope.navItems = [{
            "name": "home"
          }, {
            "name": "link1"
          }, {
            "name": "link2"
          }, {
            "name": "link3"
          }];
          timeout(function() {
            console.log($(element).width());
          })
        }

      }
    });
})();

3 个解决方案

#1


6  

link function isn't a correct place to inject dependency. It has defined sequence of parameter like I shown below. You can't put dependency there.

链接函数不是注入依赖项的正确位置。它定义了如下所示的参数序列。你不能把依赖放在那里。

link(scope, element, attrs, controller, transcludeFn) {

Inject $timeout dependency in directive function.

在指令函数中注入$ timeout依赖项。

(function() {
  angular.module('github', [])
    .directive('mynav', function($window, $timeout) { //<-- dependency injected here
      return {

Then just use injected $timeout inside link function

然后只需在链接函数中使用注入$ timeout

$timeout(function() {
    console.log(element.width());
})

#2


0  

setInterval(function(){
    // code here
    $scope.$apply();
}, 1000); 

$apply is included as a reminder that since this is an external jQuery call you need to tell angular to update the DOM.

$ apply包含在内,因为这是一个外部jQuery调用,你需要告诉angular更新DOM。

$timeout being an angular version automatically updates the DOM

$ timeout是一个角度版本自动更新DOM

#3


-1  

Just replace timeout with setinterval like below:

只需用setinterval替换timeout,如下所示:

(function() {
  angular.module('github', [])
    .directive('mynav', function($window) {
      return {
        restrict: 'A',
        link: function(scope, element, attrs, timeout) {
          scope.navItems = [{
            "name": "home"
          }, {
            "name": "link1"
          }, {
            "name": "link2"
          }, {
            "name": "link3"
          }];
          setInterval(function() { // replpace 'timeout' with 'setInterval'
            console.log($(element).width());
          })
        }

      }
    });
})();

Hope it works for you.

希望对你有效。

#1


6  

link function isn't a correct place to inject dependency. It has defined sequence of parameter like I shown below. You can't put dependency there.

链接函数不是注入依赖项的正确位置。它定义了如下所示的参数序列。你不能把依赖放在那里。

link(scope, element, attrs, controller, transcludeFn) {

Inject $timeout dependency in directive function.

在指令函数中注入$ timeout依赖项。

(function() {
  angular.module('github', [])
    .directive('mynav', function($window, $timeout) { //<-- dependency injected here
      return {

Then just use injected $timeout inside link function

然后只需在链接函数中使用注入$ timeout

$timeout(function() {
    console.log(element.width());
})

#2


0  

setInterval(function(){
    // code here
    $scope.$apply();
}, 1000); 

$apply is included as a reminder that since this is an external jQuery call you need to tell angular to update the DOM.

$ apply包含在内,因为这是一个外部jQuery调用,你需要告诉angular更新DOM。

$timeout being an angular version automatically updates the DOM

$ timeout是一个角度版本自动更新DOM

#3


-1  

Just replace timeout with setinterval like below:

只需用setinterval替换timeout,如下所示:

(function() {
  angular.module('github', [])
    .directive('mynav', function($window) {
      return {
        restrict: 'A',
        link: function(scope, element, attrs, timeout) {
          scope.navItems = [{
            "name": "home"
          }, {
            "name": "link1"
          }, {
            "name": "link2"
          }, {
            "name": "link3"
          }];
          setInterval(function() { // replpace 'timeout' with 'setInterval'
            console.log($(element).width());
          })
        }

      }
    });
})();

Hope it works for you.

希望对你有效。