Get AngularJS scope variable (inside javascript) defined in resolve/promise -
i defining rootscope variable in routes using resolve/promise want variable available before controller gets executed.
$routeprovider.when('/', { templateurl: 'test.html', controller: 'test', resolve: { ccapp: ['$q','$rootscope','$location',function($q,$rootscope,$location) { var defer = $q.defer(); $rootscope.teststring = "this test data"; defer.resolve($rootscope.teststring); return defer.promise; }] } });
and trying access in javascript as:
<script type="text/javascript" > var teststring = $('[ng-controller="test"]').scope().teststring; </script>
i didn't use rootscope inside javascript rootscope not defined in javascript.
but not working. doubt whether can use controller here. otherwise, how can value inside javascript. please help.
add $timeout give enough time scope available javascript read value.
$routeprovider.when('/', { templateurl: 'test.html', controller: 'test', resolve: { ccapp: ['$q','$rootscope','$location','$timeout',function($q,$rootscope,$location,$timeout) { var defer = $q.defer(); $rootscope.teststring = "this test data"; $timeout(function() { defer.resolve($rootscope.teststring); } return defer.promise; }] } });
and working now.
Comments
Post a Comment