angularjs-rootscope

AngularJS - Accessing ng-init variables from run method

浪尽此生 提交于 2019-12-14 03:49:35
问题 1) I have variables initialized in ng-init Eg - ng-init="password='Mightybear'"; 2) I want to access it from the .run method. Eg - anguar.module("ngApp", []) .run(function() { //Access password here }); Below scenarios I have tried, and did not work - 1) angular.module("ngApp", []) .run(function($rootScope) { console.log($rootScope.password) //undefined!!! }); 2) angular.module("ngApp", []) .run(function($rootScope, $timeout) { $(timeout(function() { console.log($rootScope.password) /

Angularjs: Uncaught ReferenceError $rootScope is not defined

假如想象 提交于 2019-12-12 05:37:34
问题 I am trying to run and got this message: Uncaught ReferenceError: $rootScope is not defined at app.js line 12 here is my js/app.js angular.module('addEvent', ['ngRoute']) .config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) { $routeProvider.when('/add-event', { templateUrl: 'views/add-event.html', controller: 'formCtrl', controllerAs: 'eventCtl' }) .otherwise({ redirectTo: '/' }); $locationProvider.html5Mode(true); }]) .run(['$rootScope', function() {

Why I can't set/get $rootScope?

有些话、适合烂在心里 提交于 2019-12-12 02:54:23
问题 I try to set a a value in $rootScope in a .run() and try to get it in a service but it looks like it's undefined. Everything else in the .run() and in the service is running fine. .run(function($ionicPlatform, $translate, $rootScope) { document.addEventListener("deviceready",function(){ onDeviceReady($rootScope); }, false); function onDeviceReady($rootScope) { if(typeof navigator.globalization !== "undefined") { navigator.globalization.getPreferredLanguage(function(language) { $rootScope

How to reset $rootScope?

感情迁移 提交于 2019-12-10 12:52:08
问题 After user logs out, I need to clean up my $rootScope. I tried $rootScope.$destroy() but that didn't do the trick. Is there a way to loop through all the values in $rootScope and delete them or a method to simply reset it? 回答1: You may wish to retain the default values that come with $rootScope when it is initialized. Since they all begin with a $ you can delete all the properties that don't start with $ . for (var prop in $rootScope) { if (prop.substring(0,1) !== '$') { delete $rootScope

Is binding objects to angular's $rootScope in a service bad?

a 夏天 提交于 2019-11-28 18:44:35
问题 In angular, I have an object that will be exposed across my application via a service. Some of the fields on that object are dynamic, and will be updated as normal by bindings in the controllers that use the service. But some of the fields are computed properties, that depend on the other fields, and need to be dynamically updated. Here's a simple example (which is working on jsbin here). My service model exposes fields a , b and c where c is calculated from a + B in calcC() . Note, in my

Best practice for using $rootscope in an Angularjs application?

浪尽此生 提交于 2019-11-27 09:33:50
We have a large Angularjs 1.6 application that has $rootscope scattered throughout the app in over 200 places in filters, services, routes, etc.. so it needs to be refactored, but I'm not sure how to know when to remove it. When is it a best practice to use $rootscope in the application? I've read everything from never, to using it for storing variables, which I assumed was for sharing data between controllers. I've since read that it's better to use factories/services for this use case instead and I also read that one valid use case is to use $rootscope as a global event bus. I didn't really

Best practice for using $rootscope in an Angularjs application?

时光总嘲笑我的痴心妄想 提交于 2019-11-26 14:46:25
问题 We have a large Angularjs 1.6 application that has $rootscope scattered throughout the app in over 200 places in filters, services, routes, etc.. so it needs to be refactored, but I'm not sure how to know when to remove it. When is it a best practice to use $rootscope in the application? I've read everything from never, to using it for storing variables, which I assumed was for sharing data between controllers. I've since read that it's better to use factories/services for this use case