2017-04-22 1 views
0

나는 ngStorage을 사용하여 체크 아웃 될 때까지 카트 데이터를 저장하고 데이터베이스에 저장 될 사이트를 확보했습니다. VM을 사용할 때 $ storage = $ localStorage; 내 컨트롤러에서는 index.html 섹션의 페이지 부분에 대한 장바구니 정보에 쉽게 액세스 할 수 있지만 내부에 중첩되어있는 ngStorage의 정보를 내 사이트 헤더에 표시하고 표시 할 수 있습니다.사이트 서식 파일에서 ngStorage 값 사용

저는 li 태그에 ng-controller = "SiteController"를 추가했지만 그 방법을 표시 할 아무 것도 얻지 못했습니다.

내 사이트의 템플릿 (index.html)에 ngStorage를 사용하여 localStorage를 표시하려면 어떻게해야합니까?

<html ng-app="myApp"> 
<body> 
// a bunch of code to show my other navigation 
// code to show my shopping cart at a glance 
    <li class="quick-cart"> 
     <a href="#"> 
     <span class="badge badge-aqua btn-xs badge-corner"> 
      {{vm.$storage.cart.items.length}} 
     </span> 
     <i class="fa fa-shopping-cart"></i> 
     </a> 
     <div class="quick-cart-box"> 
      <h4>Shop Cart</h4> 
     <div class="quick-cart-wrapper"> 
      <a href="#"><!-- cart item --> 
       <h6>Item Name</h6> 
       <small>$12.34</small> 
      </a><!-- /cart item --> 

      <!-- cart no items example --> 
      <!--<a class="text-center" href="#"><h6>0 ITEMS ON YOUR CART</h6></a>--> 
     </div> 
     <!-- quick cart footer --> 
     <div class="quick-cart-footer clearfix"> 
     <a href="shop-cart.html" class="btn btn-primary btn-xs pull-right">VIEW CART</a> 
     <span class="pull-left"><strong>TOTAL:</strong> $54.39</span> 
     </div> 
     <!-- /quick cart footer --> 
     </div> 
    </li> 
//other code between the header and controller content 
<div ng-view></div> //the CONTENT section controlled by each controller 
//other code to finish the page 

내 사이트 controller.js 코드는 다음과 같습니다 :

/* global angular */ angular.module('myApp') 
    .controller('SiteController', SiteController); 


function SiteController($route, $routeParams, AuthFactory, jwtHelper, $window, $localStorage, $sessionStorage) { 
    var vm = this; 
    vm.$storage = $localStorage; 
    vm.$storage.cart = vm.$storage.cart; 
    console.log(vm.$storage.cart); 
} 

답변

2

당신이 컨트롤러 controllerAs 구문을 사용하고 있기 때문에 당신이 사용할 필요가

내 index.html 파일은 다음과 같이 설정 무언가이다 는 as alias

ng-controller="SiteController as vm" 

ng-controller에서이 t를 정의 그는 vm에 대한보기에서 이미 사용하고 있습니다 :

{{vm.$storage.cart.items.length}} 
+0

OMG! 방금 좌절감과 머리카락을 풀어주었습니다. 고맙습니다!!! – user3561890

관련 문제