- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 AngularJs 和 ngStorage 制作购物车。我想将数据保存在本地存储中。当我从不同的浏览器选项卡添加内容时,我想在打开购物车的购物车中调用我的 http 方法。我想使用 $scope.$watch 但它不起作用:(
我尝试在本地存储中使用辅助值,第一个 Controller :
$scope.$watch('counter', function(newValue, oldValue) {
if(newValue === oldValue){
return;
}
console.log(newValue);
});
$scope.deleteItem = function (item) {
var index = $scope.storage.products.indexOf(item);
$scope.storage.products.splice(index, 1);
$scope.counter = 0;
}
第二个 Controller : $scope.addToCart = 函数(代码){
var prod = {
code: code,
quantity: 1,
color: 0,
notes: '',
};
$scope.storage.products.push(prod);
$scope.storage.counter=$scope.storage.counter+1;
$scope.$apply();
}
如何在另一个浏览器选项卡中修改我的 localstorage 值?
最佳答案
基于适合您上下文的@stackg91 引用:您可以将处理程序附加到本地存储并在每次存储更改时广播一个事件:
var app = angular.module('app',['ngResource','ngRoute','ngCookies']);
app.config(['$routeProvider',function($routeProvider){
$routeProvider
.when('/', {templateUrl: '/home.html', controller: 'myCtrl'})
.otherwise({ redirectTo: '/' });
}]).run(['$rootScope', function($rootScope) {
// you might need to inject you storage module here
if (window.addEventListener) {
window.addEventListener("storage", handler, false);
} else {
window.attachEvent("onstorage", handler);
};
function handler(e) {
// get the value from storage based on storage module you're using
$rootScope.$broadcast('counterChanged',
localStorage.getItem('counter'));
}
}]);
然后你可以在任何 Controller 中对这个事件使用react:
app.controller('firstController',['$scope', function($scope){
$scope.addToCart = function(code) {
var prod = {
code: code,
quantity: 1,
color: 0,
notes: '',
};
$scope.storage.products.push(prod);
$scope.storage.counter=$scope.storage.counter+1;
$scope.$apply();
}
}]);
app.controller('myCtrl',['$scope', function($scope){
$scope.$on('counterChanged', function(event, data) {
// do things with the new counter
console.log(data);
});
}]);
关于javascript - Angularjs ngStorage 在其他 Controller /浏览器选项卡中观看本地存储变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29651063/
我下面有使用 ngStorage 的代码。但是当我尝试运行这是我的 Angular 应用程序的一部分时,我在控制台上收到此错误: Error: [$injector:unpr] Unknown pro
我正在构建一个处理类(class)的指令,其中一部分是将类(class)数据保存在本地存储中,以便用户可以继续。 当数据改变时我调用这个函数 function setLocalStorage(){
我正在使用 ngStorage 进行网络存储。 这是我目前用来检查是否定义了 $localStorage 的代码。 if ( angular.isDefined($localStorage.setti
我正在使用 angular.js 和 ngstore。我正在使用基于 token 的身份验证和 node.js REST 服务,从 Angular 应用程序我将我的凭据发送到休息服务器并收到 toke
我正在使用 ngStorage 来存储数据。我制作了一个名为游戏的自定义对象并将其存储。它存储为游戏,但当我刷新浏览器时,它会作为对象返回。 var games= $localStorage.game
我知道有一些解决方案可以在我们的应用程序中使用 ngstorage,以便即使在刷新后也能获得持久数据。我今天第一次尝试以我的形式实现它。但无法弄清楚我哪里出了问题。有人可以让我知道我哪里出错了吗?另外
所以,我是 Angular 的新手,我正在构建一个将数据保存在浏览器的 localStorage 中的应用程序。我正在尝试为此使用 ngStorage,但出现以下错误: Uncaught Error:
我一直在构建一个 Ionic 应用程序,然后才开始实现 ngStorage .这是一种非常简单的方法,可以简单地扩展我的应用程序并存储范围内的内容。例如,我去了所有有 $scope.listOfThi
访问https://github.com/gsklee/ngStorage . 我的代码有2个部分,在partial1中我有3个输入框,在其中输入的数据是“abc”,“pqr”,“xyz”,单击按钮时
我有这个 HTML English Danish {{name[language]}} 和javascript $scope.language='en'; //initial default val
我对ngStorage有疑问。当我将我的工厂用于 jQuery 的 $ajax 时,我无法在 .then() 中使用 $localStorage (它不起作用) . $scope.updateProf
我正在使用 AngularJS 将一些数据传递到表中。表格上方是一系列复选框,用于显示/隐藏表格中的列。 当用户离开/刷新页面时,如何保留用户的复选框首选项? 我已经阅读了一些内容,我想我也许可以使用
在浏览器 session 存储中我们有 `mykey` `myvalue` 其中 mykey 位于 session 存储部分的 Key 列中和其中 myvalue 位于 session 存储部分的值列
我使用 ngstorage 在 localstorage 中保存一些值我的 Controller 是: app.controller("main", function( $scope, $localS
试图转换这个 .service('dataStore', function($localStorage,$scope){ this.entfeeds=[]; this.topfeeds
我正在使用 AngularJs 和 ngStorage 制作购物车。我想将数据保存在本地存储中。当我从不同的浏览器选项卡添加内容时,我想在打开购物车的购物车中调用我的 http 方法。我想使用 $sc
在我的 Ionic 应用程序中,我添加了插件“ngStorage”,它附带了一些演示代码: var add = function (thing) { $localStorage.things.
Chrome 在跨域 iframe 内运行时拒绝访问 localStorage。 这会导致 ngStorage 使整个应用程序崩溃 演示: http://jsfiddle.net/e8y85/ 堆栈跟
如何在本地存储中存储多个表单数据,我正在尝试在angular js中将多个表单数据存储在本地存储中。我尝试了以下代码:
我正在使用 ngStorage 处理 AngularJS 应用中的本地存储和 Protractor 到 e2e 规范。 describe('Test', function() { beforeEa
我是一名优秀的程序员,十分优秀!