angularjs - getElementByID not working in angular if ng-if is used in an element -
i have paragraph element in html. element visible if value true.i have used ng-if check that. trying access element through getelementbyid returns null.
however able access other paragraph element in html ng-if not used. how access paragraph element , value bound angular directive?
html
<!doctype html> <html ng-app="testapp"> <head> <script data-require="angular.js@*" data-semver="2.0.0-alpha.31" src="https://code.angularjs.org/1.4.0/angular.min.js"></script> <link rel="stylesheet" href="style.css" /> <script src="script.js"></script> </head> <body> <div class="pull-right" style="margin-top:50px;margin-right:100px" ng-controller="maincontroller"> <div> <p id="link" ng-if="user.isadmin">new link</p> <p id="test"> </div> {{value}} </div> </body> </html>
angular js
var app = angular.module('testapp',[]); app.controller('maincontroller', ['$scope', function($scope) { $scope.user={}; $scope.today = new date(); $scope.user.isadmin=true; $scope.value = "test123"; var objstatic = document.getelementbyid('link'); console.log(objstatic);//returns null var objdynamic = document.getelementbyid('test'); console.log(objdynamic); objdynamic.innerhtml="dynamic test"; }]);
working copy updated here working demo
the problem you're trying access element before digest cycle has rendered it. if change code use $timeout see element
$timeout(function() { var objstatic = document.getelementbyid('lnkmanage'); console.log(objstatic); }, 0);
Comments
Post a Comment