如何在角度js

时间:2016-11-04 15:20:30

标签: javascript java angularjs

在我的主屏幕上,我通过调用角度控制器(SeacrhController)来获取一些数据,然后单击主页上的一个按钮(开始),打开另一个选项卡。在第二个选项卡上单击完成按钮,第二个选项卡是关闭并刷新父页面。

我希望早些时候在我的父页面上搜索过数据。任何人都可以告诉我,我怎样才能实现这一点。如何保持会话。

我尝试过Cookies和localStorage,但无法让事情发生。任何帮助都可以。

common.js:

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

myApp.controller('SearchController', function($scope,$http) {
      $scope.clientVO = {};

      $scope.getClientDetail = function(bcidNo) {
          var response=$http.get('/testWeb/rest/clientData/'+ id);

          response.success(function(data) {
                console.log("getActor data: " + angular.toJson(data, false));
                $scope.clientVO = data;
               })

              response.error(function(data, status, headers, config) {
                alert("AJAX failed to get data, status=" + status);
              })

      }
    });

home.html:

<!DOCTYPE html>
<html ng-app="MyApp">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.min.js"></script>
<script src="../javascripts/common/common.js"></script>
</head>
<!--some code -->
<div ng-controller="SearchController">
    <form class="content-left-body">
<div class="client-details">
        <span class="content-right-header">Client Details</span>


            <table>
                        <tr><td>
                            <label>Client ID:</label></td>
                            <td><input type="text" name="id" id="Search"
                                placeholder="Search" ng-model="id" class="search-box"/> 
                            <img alt="Search" src="../images/search_img.png" ng-click= "getClientDetail(id);" style="margin-left:-15% ; margin-top:2.5%">


                            </td>
                            <td>
                        <div ng-show="clientVO.clientName==''"><label style="color:red">ID not found in database.</label></div>
                        </td>
                    </table>
            <div>

<div>
            <table style="width: 100%">
                <tr >
                    <td><label>Client Name:</label></td>
                    <td><span ng-show="!clientVO.clientName">-</span><label ng-bind="clientVO.clientName"></label></td>
                    <td><label>AccNo:</label></td>
                    <td><input type="text" ng-disabled="!clientVO.clientName"></td>
                </tr>
                <tr >
                    <td><label>Contact Name:</label></td>
                    <td><span ng-show="!clientVO.contactName">-</span><label ng-bind="clientVO.contactName"></label></td>
                    <td><label>Validation Level:</label></td>
                    <td>
                        <select ng-disabled="!clientVO.clientName">
                            <option value="">-Please Select-</option>
                            <option value="">Level 1</option>
                            <option value="">Level 2</option>
                            <option value="">Level 3</option>
                            <option value="">Level 4</option>
                        </select>
                    </td>
                </tr>
        </table>
    </div>

    <div class="Data-details">
        <div class="data">
            <div class="content-left-body">
                <span class="content-left-header">Data details</span>
                <span class="content-left-header-small">Data classification</span>                  
                <label id="clientClassification" ></label>  <br><br>
                <button id="btn-yellow"  onClick=window.open("classification.html","fullscreen=yes");>Start</button>
            </div>
        </div>
    </div>  
</form>

<!---some code -->
</html>

1 个答案:

答案 0 :(得分:1)

我有这个简单的服务来存储localStorage中的数据(请注意,localStorage在SO中不起作用)

angular.module('app', [])
.controller('AppCtrl', ['StorageSrv', function(StorageSrv){
  StorageSrv.set('user', {first: 'John', last: 'Doe'})
  
  console.log(StorageSrv.get('user'));
}]);

// This service can live in a separate file

  angular.module('app')
  .service('StorageSrv', ['$rootScope', function ($rootScope) {
    var self = this,
    prefix = 'your_prefix',
    ls = window.localStorage;

    self.set = function(key, val){
      var obj = {};
      _.set(obj, key, val);
      ls.setItem(prefix, JSON.stringify(obj));
    };

    self.get = function(keyPath){
      if (!keyPath || !_.size(keyPath))
        return JSON.parse(ls.getItem(prefix));
      else
        return _.get(JSON.parse(ls.getItem(prefix)), keyPath, null);
    };

    self.delete = function(keyPath){
      var key = prefix + '_'+_.trimStart(key, prefix);
      var current = self.get(key);
      _.unset(current, keyPath);
      if (!_.size(current)){
        self.update(key, {})
      }
      else {
        self.update(key, current);
      }
    };
  }])
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="app" ng-controller="AppCtrl"></div>