I try to get a width from a $element in directive. but, it continues to return 0.
Can you tell me what is the problem? and, can you tell me the solution?
To see problem, i've created a demo :
directive
angular.module('testApp').directive('testDirective', testDirective) function testDirective() { return { restrict: 'E', controller: 'TestController', controllerAs: 'vm', bindToController: true, scope: {} } }
controller
angular.module('testApp').controller('TestController', TestController); TestController.$inject = ['$element']; function TestController($element) { var vm = this; init() function init(){ vm.elWidth0 = $element.find("div")[0].clientWidth; vm.elWidth1 = $element.find("div")[0].offsetWidth; vm.elWidth2 = $element.prop('clientWidth'); console.log(vm.elWidth0); //return 0 console.log(vm.elWidth1); //return 0 console.log(vm.elWidth2); //return 0 } }
html
<test-directive> <div> <button>a</button> <button>c</button> <button>d</button> <button>e</button> <button>f</button> </div> </test-directive>
Thank you for your advice