(function() { 'use strict'; angular.module("app") .directive('myDirective', [getmyDirective]); function getmyDirective() { try { var directiveDefinitionObject = { template: '<dir class="myDirective" ></dir>', replace: true, transclude: false, restrict: 'AE', scope: false, controller: function($scope, $element, $attrs) { $scope.$watch(function() { return document.documentElement.clientWidth; }, function(newValue, oldValue) { SetSize(); } ); function SetSize() { $scope.width = $element.width(); } }, compile: function compile(tElement, tAttrs, transclude) { return { pre: function preLink(scope, iElement, iAttrs, controller) {}, post: function postLink(scope, iElement, iAttrs, controller) { setTimeout(function() { scope.SetSize(); }, 1000); } } }, } return directiveDefinitionObject; } catch (e) { console.log(e); } } })(); I need to get the width of the block occupied by the $ scope.width directive. In the block properties, the width is 100% of the parent block.
Initially, after downloading the site directive is missing on the page. After loading, it has the display: hidden property (located under the spoiler).
As can be seen from the code, the width is measured as late as possible, 1 second after the compile postlink is triggered. However, the measurement result is $ scope.width = 100.
When the window is resized, after the directive has become visible,
$scope.$watch(function() { return document.documentElement.clientWidth; }) measuring $ scope.width gives real results.
Question: What are the $ element events that $ watch could be hung to track the directive's actual width? Where can I read it? The decision to hang events on $ ('. MyDirective') is not appropriate, the directive is used repeatedly, the sizes of elements are different.
Passing an external parameter is also not suitable. The width of the element must be determined by the directive itself.
Example:
var app = angular.module('myApp', []); app.controller('personCtrl', function($scope) { $scope.isOpen = false; }); app.directive('myDirective', [ getmyDirective]); function getmyDirective(){ var directiveDefinitionObject = { template: '<dir class="myDirective" ><dir ng-style="dstyle[size]">window1 {{width}}</dir><dir ng-style="dstyle[size]">window2 {{size}}</dir></dir>', replace: true, transclude:false, restrict: 'AE', scope: false, controller: function($scope, $element, $attrs ) { $scope.$watch(function() { return document.documentElement.clientWidth; }, function(newValue, oldValue) { SetSize(); } ); $scope.SetSize = SetSize; $scope.dstyle = [ { 'width': '25%' , 'float': 'left'}, { 'width': '100%' } ]; function SetSize(){ $scope.width = // 150; $element.width() ; $scope.size= ($scope.width > 200 )? 0: 1; } }, compile: function compile(tElement, tAttrs, transclude) { return { pre: function preLink(scope, iElement, iAttrs, controller) {}, post: function postLink(scope, iElement, iAttrs, controller) { setTimeout(function(){ scope.SetSize(); },1000); } } }, } return directiveDefinitionObject; } .container { width: 100vw} .inside {width: 100%} <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <!DOCTYPE html> <html> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script> <body> <div ng-app="myApp" ng-controller="personCtrl"> <button ng-click="isOpen=!isOpen" >Open</button> <div ng-if="isOpen" class="container"> <div data-my-directive class="inside"></div> </div> </div> </body> </html>
try...catch? - Grundy$elementis a jqLite object, its events are the same as those of a jQuery Object, what side is there a$watchthat is in$scope? - Grundy