我有一个javascript对象,我需要全局到我的angularjs应用程序.当应用程序即将卸载时(由于页面刷新或其他一些情况),我想将该对象持久保存到浏览器存储.我相信将对象添加到$ rootScope将满足使对象成为全局的第一个要求,但是在onbeforeunload事件期间将对象写入存储将需要访问$ rootScope变量.是否可以在angularjs中检测页面卸载事件?
我有角度控制器仪表板并在仪表板中设置根范围值并使用范围访问仪表板模板内部组件中的根范围值.但我不知道这是否正确..我无法得到值
function DashBoardController($http, $window, $rootScope, apiurl, $scope, $location,$interval) {
var ctrl = this;
ctrl.$onInit = function () {
getUser();
};
function getUser(){
$http({
method: 'GET',
url: apiurl + '/getUser',
}).success(function (data, status) {
if (data.status == true) {
$rootScope.user = data.result;
$rootScope.test = "TEST";
}
});
}
function Controller1($rootScope,$scope, $timeout,$http, apiurl,$interval) {
var ctrl = this;
$scope.value = $rootScope.test;
alert($scope.value);
$scope.value1 = $rootScope.user;
console.log($scope.value1);
}
app.module('app').component('component1', {
templateUrl: '../resources/views/component/component1.html',
controller: Controller1
});
})(window.angular);
Run Code Online (Sandbox Code Playgroud)
我没有定义
我正在使用AngularJS $rootScope对象来公开一些需要控制器和视图都可访问的全局常量:
var app = angular.module('myApp', []);
app.run(function ($rootScope) {
$rootScope.myConstant = 2;
});
Run Code Online (Sandbox Code Playgroud)
当我尝试在视图中呈现全局值时,它可以正常工作:
{{myConstant}}
Run Code Online (Sandbox Code Playgroud)
同样,如果我在ng-if条件中引用全局值,它也可以工作:
<span ng-if="someValue == myConstant">Conditional content</span>.
Run Code Online (Sandbox Code Playgroud)
但是,当尝试在ng-switch块中使用相同的值进行比较时,它永远不会计算为true.这个JSFiddle演示了我尝试让它工作的尝试.我也尝试显式引用常量值作为$rootScope表达式的成员和表达式(在双花括号内)但没有任何作用.
我有什么想法我做错了吗?
谢谢,
蒂姆
所以我有一个AngularJS服务监听一些事件.在处理这些事件时,我需要调用不同的控制器并最终加载新视图.在一个事件处理程序中,我使用$ location.path()然后调用$ rootScope.apply()来触发到控制器的路由.这适用于该事件,但在其他情况下,我收到以下错误:$rootScope:inprog Action Already In Progress.我猜测它在第一个场景中有效,因为$ rootScope.apply()是从侦听器函数内的另一个回调函数调用的,其他处理程序试图仅从事件侦听器函数调用它.
//angular service
$rootScope.$on('MY_EVENT', function (event, msg) {
MyClass.doSomething(msg, function (response) {
$location.path("/view1");
$rootScope.$apply(); //WORKS FINE
});
});
$rootScope.$on('MY_OTHER_EVENT', function (event, msg) {
$location.path("/view2");
$rootScope.$apply(); //ERROR
});
Run Code Online (Sandbox Code Playgroud)
如何让它适用于所有事件处理程序?
向AngularJS再次向大家寻求帮助.我正在构建一个SPA,其中我的布局(主)页面被划分为三个部分左侧导航栏,中心内容,右侧项目列表栏.我加载主页时应隐藏正确的项目列表,但应在剩余的屏幕中显示.我在homeController中创建了一个$ rootScope变量,并根据位置路径将值设置为'true',并在模板中使用该变量将值设置为ngHide.当我使用SPA导航到另一个页面时,我的右侧和左侧栏不会再次加载,只有我的中心内容会执行新视图.在将数据发送到新视图模板的控制器中加载新视图时,我将我在homeController中创建的$ rootScope变量重置为' 假',但我的右栏仍然不可见.虽然interpollation已将值更新为'true',但我可以看到ng-hidden类仍在使用.任何建议都将受到高度赞赏.
布局页面标记:
<aside class="right_bar" ng-hide="{{$root.show}}">
<div class="my-list"><span class="f3">My Cart</span><div class=""><span class="list-count">0</span></div></div><div class="list-item"><ul></ul></div>
</aside>
Run Code Online (Sandbox Code Playgroud)
homeController代码:
function getHomeConfigsCtrl($http, $location, $rootScope) {
$rootScope.show = false;
var loc = $location.path();
if (loc === '/Home/Index' || loc ==='')
{
$rootScope.show = true;
}
}
Run Code Online (Sandbox Code Playgroud)
类别控制器代码:这是我重置$ rootScope变量值的地方
function getAllCategoryDetails($routeParams,$rootScope) {
$rootScope.show = false;
}
Run Code Online (Sandbox Code Playgroud) $ rootScope和$ rootScope之间有什么区别.$ root?
有什么区别
$ rootScope.global.flag = true和$ rootScope.$ root.global.flag = true
他们俩都在rootcope中访问同一个变量吗?
如果是这样,是否有任何特殊情况我们必须使用它们中的任何一个?
HTML文件是这样的:
<!DOCTYPE html>
<html lang="en" ng-app="myApp">
<head>
<meta charset="utf-8">
<title>HTTP Request</title>
<script src="angularjs"></script>
<script src="appjs"></script>
</head>
<body>
<div ng-controller="myCtrl1">
First Name: <input type="text" ng-model="fname" required>
Last Name: <input type="text" ng-model="lname" required>
<button ng-click="search()">Send HTTP Request</button>
<p>Searching for : {{fname}} {{lname}}</p>
<p>Response Status: </p>
<p>{{status}}</p>
<p>Data: {{data}}</p><br>
<p>Total number of results : {{data.numResults}}</p>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我写了一个控制器:
var myApp = angular.module('myApp', []);
myApp.controller('myCtrl1', ['$rootScope', '$http', function($rootScope, $http) {
$rootScope.fname = "";
$rootScope.lname="";
$rootScope.search = function() {
var search_url = "/search?fname=" + …Run Code Online (Sandbox Code Playgroud) 我有一个场景,我需要非角度代码(在这种情况下Facebook和Twitter JS sdk)来广播角度控制器可以收听的事件.为什么?在初始页面加载后,我通过AJAX加载和修改页面中的内容.内容有多个喜欢/推特按钮(如博客/聚合器的首页).我想在Facebook"喜欢"按钮或Twitter"分享"按钮上发生点击时添加事件挂钩 - 页面中有一些,每当获取新内容时每个都在变化(再次,通过ajax ).
现在,SDK的处理方式不同:
twttr.events.bind('click', function(event){
// here I'd like to somehow
// $rootScope.$broadcast("twitter:clicked", event.currentTarget)
});
Run Code Online (Sandbox Code Playgroud)
FB.Event.subscribe("edge.create", function(resp) {
// here I'd like to somehow
// $rootScope.$broadcast("facebook:clicked", resp)
});
Run Code Online (Sandbox Code Playgroud)
app.controller("SomeController", ['$scope', function($scope) {
$scope.$on("facebook:clicked", function(event, resp) {
// do something with resp
});
$scope.$on("twitter:clicked", function(event, target) {
// do something target
});
}])
Run Code Online (Sandbox Code Playgroud)
我们的想法是绑定这些事件处理程序,然后处理相关控制器中的广播消息,即使每次获取新内容时都会重新创建这些控制器.
所以基本上,我正在寻找的是一种挂钩现有的已经初始化的角度应用程序$rootScope和$broadcast消息的方法,尽管它不是应用程序的一部分.希望这是有道理的.谢谢.
嘿我如何缓存x时间这个我通过$ http($ rootScope.config.app_genres)设置的简单对象?
$http.get($rootScope.config.app_ws+'get/genres',{},{cache:true}).success(function(response) {
$rootScope.config.app_genres = response;
});
Run Code Online (Sandbox Code Playgroud)
我只想缓存它,以便每次http请求都不重复
我在运行中定义了一个$ rootscope对象,如下所示。
//Start the AngularJS App
var angularApp = angular.module('angularApp')
//Run at the start of the Angular Application
.run(function ($rootScope, $location, $firebase, $firebaseSimpleLogin) {
//Create the User Object.
$rootScope.user = $firebase($rootScope.fb.child('/persons/person1'));
//this works fine!
console.log($rootScope.user.userId)
});
Run Code Online (Sandbox Code Playgroud)
$ rootScope.user包含userId,userName,userEmail等。
稍后,在控制器中,我想在$ rootScope.user中使用一些数据,但是它是未定义的!
function myCtrl($rootScope, $scope) {
//need to use $rootsope.user.userId in this command...
$scope.folders = $firebase($rootScope.fb.child('/persons/' + $rootScope.user.userId);
//this will display the $rootScope in the console no problem!
console.log($rootScope);
//this just displays 'undefined'
console.log($rootScope.user);
};
Run Code Online (Sandbox Code Playgroud)
令我发疯的是,当myCtrl在chrome javascript控制台中显示$ rootScope时,我可以展开对象并查看用户对象!
但是第二个console.log只是未定义的。请帮忙!