asynchronous - AngularJS - Wait for View HTML to Load Before Executing Variable Assignment? -
i have page in angularjs looks like
<body ng-controller="maincontroller"> <my-view></my-view> </body> inside my-view file, have input element:
<div> <input id="my-input" type="file"> </div> in maincontroller, assign file input element $scope variable. in 50% of page loads, $scope variable assigned null value. assume because element hasn't been loaded onto dom when controller assigns variable.
i tried using
angular.element(document).ready(function(){ $scope.filechooser = document.getelementbyid('my-input'); }); but doesn't seem solve problem since request my-view html file asynchronous , made after initial page load. found temporary solution wrapping variable assignment inside of settimeout(), i'd avoid this.
is there built-in way in angular wait views load before performing code execution?
i solved problem doing variable assignment inside of directive instead of controller.
myapp.directive('myview', function(){ return{ restrict: 'e', replace: true, scope: '=', templateurl: '/path/to/html.html', link: function(){ // assign input element scope variable scope.filechooser = document.getelementbyid('my-input'); } } });
Comments
Post a Comment