angularjs cookies有些问题

时间:2017-07-03 09:06:59

标签: javascript angularjs cookies

我正在尝试保存计算器选项卡的值,当您重新加载页面并设置按钮以保存并将值放入计算器(sry for broken english)。 我无法链接计算器的标签和Cookie。

> <!DOCTYPE html>
<html ng-app="app">

<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<script src="app.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular-cookies.js"></script>
</head>


<body ng-controller="kyk">
  <p>{{ $cookies.get('cookie') }}</p>
  <input type="text" ng-model="cookieVal">
  <button ng-click="setCookie(cookieVal)">update</button>


 <div ng-controller="CalculatorController">
 <br> <span  id="myFavorite" >{{formula.join('')}}</span> <br><br><br>


  <button ng-click="add(7)">7</button>
  <button ng-click="add(8)">8</button>
  <button ng-click="add(9)">9</button>
  <button ng-click="add('/')">/</button><br>
  <button ng-click="add(4)">4</button>
  <button ng-click="add(5)">5</button>
  <button ng-click="add(6)">6</button>
  <button ng-click="add('*')">*</button><br>
  <button ng-click="add(1)">1</button>
  <button ng-click="add(2)">2</button>
  <button ng-click="add(3)">3</button>
  <button ng-click="add('-')">-</button><br>
  <button ng-click="add(0)">0</button>
  <button ng-click="add('.')">.</button>
  <button ng-click="add('+')">+</button>
  <button ng-click="eval()">=</button> <br>
  <button ng-click="remove()">CLEAR</button>
  <button ng-click="save">SAVE</button>
  <button ng-click="put(formula.join(''))">PUT</button>
  </div>

</body>

</html>
app = angular.module('app',['ngCookies',])

app.controller('kyk', function ($scope, $cookies) {
  $scope.$cookies = $cookies;
  $scope.myCookieVal = $cookies.get('cookie');
  $scope.setCookie = function (val) {
   $cookies.put('cookie', val);
  };
});

app.controller('CalculatorController', function ($scope, $cookies) {
$scope.$cookies = $cookies;
    $scope.formula = [0];
    $scope.add = function (item) {
        if($scope.formula == 0) $scope.formula = [item];
        else $scope.formula.push(item);
    };
    $scope.remove = function () {
        $scope.formula.pop();
        if($scope.formula = ['']) $scope.formula = [0];
    };

    $scope.eval = function () {
        var result = eval($scope.formula.join(''));
        $scope.formula = [result];
         $cookies.get('formul', $scope.formula.join(''));
        $cookies.put('formul', )  
    };
 $scope.save = function() {
    $cookies.getObject('save');
 };
 $scope.put = function (value) {
    $cookies.putObject('save', value);
    if($scope.formula == 0) $cookies.putObject ('save', value);
        else $scope.formula.push($cookies.putObject('save', value));
 }

});

0 个答案:

没有答案