2014-09-23 51 views
0

我被阻止,不能如下,看看有人能幫助我......(抱歉我的英文不好,我的解釋)。如何保存ng-include的值並在以後使用

我有這個筆,我有一個輸入來插入一個值或加載默認值。 NgStorage我用來存儲內存中的值,如果應用程序重新加載,並始終加載輸入(localStorage)中的用戶類型。

當我有一個下拉列表,其中我選擇了兩個數字中的一個,這兩個數字是我在以下div中創建的兩個模板之一(或template1 template2)。

極右的,但現在我想以任何方式爲下一個div(紅色邊框)保存結果,使用從驅動器開始聲明的範圍變量執行上面選擇的簡單算術運算。

請幫忙嗎?

這裏我的筆(我用的離子):http://codepen.io/maestromutenrroy/pen/ukwIC

<html ng-app="ionicApp"><head> 
    <meta charset="utf-8"> 
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width"> 

    <title>Ionic....</title> 

    <link href="//code.ionicframework.com/1.0.0-beta.12/css/ionic.css" rel="stylesheet"> 
    <script src="//code.ionicframework.com/1.0.0-beta.12/js/ionic.bundle.js"></script> 
    <script src="https://rawgithub.com/gsklee/ngStorage/master/ngStorage.js"></script> 
    </head> 
    <body ng-controller="MainCtrl"> 

    <ion-header-bar class="bar-positive"> 
     <h1 class="title">Hello!</h1> 
    </ion-header-bar> 

    <br /><br /><br /> 
    <!-- load 11, but I change the valor --> 
    Write a number: <input type="number" ng-model="$storage.c6" style="border: 1px solid #ddd; padding-left: 10px"> 
    <br> 
    <!-- select of two templates --> 
    Select one option: 
    <select ng-model="template" ng-options="t.name for t in templates"> 
     <option value="">(blank)</option> 
    </select> 
    <!-- print a template select --> 
    <div ng-include src="template.url" onload='myFunction()'></div> 

    <!-- template1.html --> 
    <script type="text/ng-template" id="template1.html"> 
     {{1000 - $storage.c6}} 
    </script> 

    <!-- template2.html --> 
    <script type="text/ng-template" id="template2.html"> 
     {{10 - $storage.c6}} 
    </script> 

    <br> 
    <hr /> 
    <div style="border: 2px solid red; padding: 20px"> 
     Now, I would like a same operation (result of template1 or template2 - $storage.c12): ..... 
    </div> 

    </body> 
</html> 

    angular.module('ionicApp', ['ionic','ngStorage']) 

    .controller('MainCtrl', function($scope, $localStorage) { 

    $scope.$storage = $localStorage.$default({ 
     c6: 1, 
     c12: 2 
    }); 

    $scope.templates = [{ 
     name: '5', 
     url: 'template1.html'} 
     , 
     { 
     name: '10', 
     url: 'template2.html'}]; 
    $scope.template = $scope.templates[0]; 

}) 

謝謝!

回答

1

可以保存在scope對象的結果,並更新每個模板裏面:

<!-- template1.html --> 
<script type="text/ng-template" id="template1.html"> 
    {{results.x = 1000 - $storage.c6}} 
</script> 

<!-- template2.html --> 
<script type="text/ng-template" id="template2.html"> 
    {{results.x = 10 - $storage.c6}} 
</script> 

<div style="border: 2px solid red; padding: 20px"> 
    Now, I would like a same operation (result of template1 or template2 - $storage.c12): ..... {{results.x - $storage.c12}} 
</div> 

和控制器上:

$scope.results = {}; 

Demo

+0

謝謝你!耶士是工作!!! – 2014-09-23 14:52:00

相關問題