初心者です
JavaScriptのフレームワークのAngularJSを少しだけ勉強しました
勉強のため簡単に電卓のアプリでも作ろうと思ってなんとなく途中までコードを書いたのですが、サービスとか役割分担?あってるでしょうか
使い方とかこうした方が良いよとかアドバイスが欲しいです
###該当のソースコード
HTMLのコード(とりあえずbodyだけ)
<p> <ons-input id="text" name="text" modifier="underbar" ng-value = "{{text}}" ng-model="text"></ons-input> </p> <!-- 4 * 4のボタンテーブル --> <table> <tr ng-repeat="col in btns"> <td ng-repeat="row in col"> <section> <ons-button modifier="large" ng-click="onclick(row)">{{row}}</ons-button> </section> </td> </tr> </table>
JavaScriptのコード
angular.module('myApp', ['onsen','ngStorage'] )
//ボタン .value('btnsValue', [ [7, 8, 9, '÷'], [4, 5, 6, '×'], [1, 2, 3, '-'], ['.',0,'=', '+'], ]) //計算とか サービス .service('calcService',function() { this.text = ''; this.num1; this.num2; this.plus = function() { } this.minus = function() { } this.multiplied = function() { } this.divided = function() { } }) //コントローラ .controller('MyController', ['$scope','btnsValue','calcService', function($scope,btnsValue,calcService) { $scope.btns = btnsValue; //クリック $scope.onclick = function(num) { //押したボタンが数字かどうか if(!isNaN(num)) { //演算子ボタンを押した } else { switch(num) { case '÷': $scope.text = calcService.plus(); break; case '×': $scope.text = calcService.minus(); break; case '-': $scope.text = calcService.multiplied(); break; case '+': $scope.text = calcService.divided(); break; } } } }]);
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/03/18 10:37
2017/03/19 08:58
2017/03/19 10:40
2017/03/19 12:13