本文介绍了如何使用ngStorage在angularjs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

https://github.com/gsklee/ngStorage 。

我的code有2个谐音,在partial1我有3个输入框,在其中输入的数据是ABC,焊接工艺评定,某某和按钮的点击我想重定向到哪里partial2输入框被填入下列控制器中的abcpqr'​​计算的细节,ABXY。

My code has 2 partials, in partial1 I've 3 input box and data entered in them be 'abc', 'pqr', 'xyz' and on click of button I want to get redirected to partial2 where input box gets populated with following details calculated in a controller 'abcpqr', 'abxy'.

这两个谐音使用localStorage的[ngStorage],并在控制器计算得到这些价值和被推到partial2按钮的点击[NG点击=使用functionName()]。此函数具有逻辑来执行计算。如何做到这一点?

Both partials uses localStorage [ngStorage] and in controller these values get calculated and gets pushed to partial2 on click of button [ng-click="functionName()"]. This function has logic to perform the calculations. how to do this?

在应用我创建我20+在两个谐音等领域,所以我不希望从那里穿过和值,而让他们存储在localStorage的访问。

In the app I'm creating i've 20+ such fields in both partials, so I don't want to pass values rather get them stored in localStorage and access from there.

推荐答案

假设你有 NG-模型属性为所有你想捕捉领域partial1像下面。

Assuming that you have ng-model attributes for all the fields that you're trying to capture in partial1 like below.

部分-1

<input type='text' ng-model='pageData.field1' />
<input type='text' ng-model='pageData.field2' />
<input type='text' ng-model='pageData.field3' />

app.js

    var myApp = angular.module('app', ['ngStorage']);

    myApp.controller('Ctrl1', function($scope, $localStorage){

        $scope.pageData = {};

        $scope.handleClick = function(){
           $localStorage.prevPageData = $scope.pageData;
        };

    });

    myApp.controller('Ctrl2', function($scope, $localStorage){

        $scope.pageData = $localStorage.prevPageData;

    });

部分-2

<p>{{pageData.field1}}</p>
<p>{{pageData.field2}}</p>
<p>{{pageData.field3}}</p>

另一个方法:

但如果你只是想从一个控制器在第1页发送数据到第2页另一个控制器,你可以做到这一点与服务也是如此。

But if you just want to send data from one controller in page1 to another controller in page2, you can do that with a service as well.

myApp.service('dataService',function(){

   var cache;

   this.saveData = function(data){
      cache = data;
   };

   this.retrieveData = function(){
      return cache;
   };

});

在你的第一个控制器

注入该服务保存页面数据,并在第二个控制器再次注入它来检索保存的页面数据。

Inject this service in your first controller to save the page data and inject it again in your second controller to retrieve the saved page data.

myApp.controller('Ctrl1', function($scope, dataService){

   dataService.saveData($scope.pageData);

});

myApp.controller('Ctrl2', function($scope, dataService){

   $scope.pageData = dataService.retrieveData();

});

这篇关于如何使用ngStorage在angularjs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-25 21:54