rootscope

Angular - can I $broadcast a message from outside an angular app?

谁都会走 提交于 2020-01-03 11:50:51
问题 I have a scenario where I need non-angular code (in this case Facebook and Twitter JS sdk's) to broadcast an event that angular controllers can listen to. Why? After the initial page load, I load and revise content in the page via AJAX. The content has multiple like/tweet buttons attached (like a front page of blog/aggregator). I'd like to add event hooks when a click occurs on either Facebook "Like" button or Twitter "Share" button - there are a few of those in the page, and each is changing

Prevent $rootScope.$on from calling function several times

风格不统一 提交于 2020-01-01 12:33:14
问题 I have a $rootScope.$on code on top of a controller. I noticed that everytime I load/call this controller, the $rootScope.$on listener increases meaning it will add and add and add a listener, indefinitely as you visit the controller. I noticed it when I called it via $rootScope.$emit from another controller, the function inside the $rootScope.$on got executed several times even if it was only a single emit/broadcast. $rootScope.$on('listen', function() { $scope.displayString(); }); $scope

Why is $http.get() undefined in my Angular service?

≯℡__Kan透↙ 提交于 2019-12-23 02:45:25
问题 I'm trying to load some JSON and store it using $rootScope so that it persists between controllers. When I run my app I get the following error: TypeError: Cannot call method 'get' of undefined The get method was working perfectly until I tried to introduce $rootScope... any ideas? My service looks like this: 1 /** 2 * Service to get the quiz data from a JSON source 3 */ 4 app.factory('quizService', ['$rootScope', function ($scope, $rootScope, $http) { 5 var promise; 6 var service = { 7

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

Angular $broadcast only updating after page refresh

强颜欢笑 提交于 2019-12-11 20:20:49
问题 Hi I have a problem in Angular where $scope.$broadcast is only updating a listening value once the page has been refreshed once: One controller: $rootScope.$broadcast('myVar', myValue); Listener: $scope.$on('myVar', function(event, myValue) { console.log(myValue); $scope.myValue= myValue; }); It works if I refresh the page once, but on initial load the listener is not working? Any ideas? 回答1: I have solved the problem, using $window.location.reload(); it appeared that I needed to reload the

How do you set $rootScope within Angular Service function?

限于喜欢 提交于 2019-12-11 10:36:11
问题 I'm having trouble setting $rootScope for Angularjs. Below is my function App.controller('Controller', function (UtilityService, $rootScope) { var setSession = function () { $rootScope.test = "yes"; // <-- I get this UtilityService.getSession().success( function () { $rootScope.test = "No"; // <-- I don't get this How do I get/set this value? }); }; setSession(); }); Additional Info: One of the ways that might work is to set up a service that is interacted between multiple controllers. Does

Storing data in $rootScope

ε祈祈猫儿з 提交于 2019-12-11 08:37:32
问题 Is it advisable to store data in $rootScope. I have a cordova app which uses sensor data which is coming every 100ms. For me to use that data in multiple controller I am using $rootScope.sensorData variable which is being refreshed every 100ms. Is it alright to use it this way? Is there a better way to do it? Thank you 回答1: You can store it in factory. In AngularJS factory is singleton, so only instance is created. myApp.factory('SensorSrv', function SensorSrv() { var sensorData; return {

Detecting page unload in angularjs

别来无恙 提交于 2019-12-10 12:58:44
问题 I have a javascript object which I need global to my angularjs application. When the application is about to be unloaded (due to a page refresh or some other scenario), I would like to persist the object to browser storage. I believe adding the object to $rootScope would fulfill the first requirement of making the object global, but writing the object to storage during the onbeforeunload event, would require access to the $rootScope variable. Is it possible to detect a page unload event in

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

Redirect state in angularjs

不羁岁月 提交于 2019-12-06 22:32:05
问题 This is the state configuration: angular .module('grabhutApp', [...]) .config(function ($stateProvider, $urlRouterProvider) { $stateProvider // ACCOUNT .state('account', { abstract: true, url: '/account', templateUrl: 'index.html' }) .state('account.main', { url: '', templateUrl: 'views/account/account.login.html', controller: 'AccountController' }) . . . // NO VIEWS .state('nv', { abstract: true, url: '/nv' }) .state('nv.logout', { url: '/logout' }) }); The nv and its sub states will have no