angularjs ionic app从数据库中添加两个数字,从输入中添加其他数字

时间:2017-04-06 12:42:46

标签: javascript angularjs ionic-framework backand

我试图在我的应用程序中添加两个数字。一个数字来自数据库,其他我希望在应用程序中插入。这很简单,但我是angularjs的新手。这是我的代码:

Html:

<div class="item-input-inset">
 <label class="item-input-wrapper">
    <input type="number" placeholder="Insert Points" ng-model="inp"/>
   </label>
   </div>
   <span> {{sum()}} </span>app.js

app.js

.controller('AppCtrl', function($scope, PointService) {
$scope.points = 0;
$scope.inp = 0;

$scope.sum = function(){ 
return $scope.points + $scope.inp }

所有应用程序显示都是数字9,这是$ scope.points,因为在我的数据库中它是9但是当我尝试输入任何数字时它不会添加输​​入数字。有人可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

我根据您的代码制作了一个工作演示,它似乎工作正常。

function ctrl($scope, $timeout) {
  $scope.points = 0;
  $scope.inp = 0;
  $scope.sum = function() {
    return $scope.points + $scope.inp;
  }
  
  getMyPoints();

  function getMyPoints() {
    // Mock service
    $timeout(function() {
      $scope.points = 9;
    }, 1000);
  }

}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js"></script>
<div ng-app>
  <div ng-controller="ctrl">
    <div class="item-input-inset">
      <label class="item-input-wrapper">
    <input type="number" placeholder="Insert Points" ng-model="inp"/>
   </label>
    </div>
    <span> {{sum()}} </span>app.js
  </div>

答案 1 :(得分:0)

这是整个app.js删除点功能还没有完成

 // Ionic Starter App
// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a          <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
angular.module('starter', ['ionic', 'backand'])

.config(function (BackandProvider) {
BackandProvider.setAppName('dozr');
BackandProvider.setAnonymousToken('00890966-560c-49a9-96af-8203d8645186');
})

 .controller('AppCtrl', function($scope, PointService) {
$scope.points = 0;
$scope.inp = 0;



 function getMyPoints() {
 PointService.getPoints()
 .then(function (result) {
  $scope.points = result.data.name;
 });
 }

 $scope.sum = function(){ 
 return $scope.points + $scope.inp }



 $scope.addPoint = function() {

  PointService.addPoint($scope.sum)
 .then(function(result) {
  $scope.input = 0;
  // Reload our points, not super cool
  getMyPoints();
});
}

//$scope.deletePoint = function(id) {
//PointService.deletePoint(id)
//.then(function (result) {
  // Reload our points, not super cool
  //getAllPoints();
// });
//}

// getMyPoints();
//})

.service('PointService', function ($http, Backand) {
var baseUrl = '/1/objects/';
var objectName = 'points/';



function getUrl() {
return Backand.getApiUrl() + baseUrl + objectName;
}

function getUrlForId(id) {
return getUrl() + id;
}

getPoints = function (id) {
return $http.get(getUrlForId(1));
};

addPoint = function(point) {
return $http.put(getUrlForId(1), point);
}

deletePoint = function (id) {
return $http.delete(getUrlForId(id));
};

return {
getPoints: getPoints,
addPoint: addPoint,
deletePoint: deletePoint
}
});