基本上,我想在 angular 操作 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());
})
}
}
});
})();