angularjs ionic app add two numbers one from database and other from input

870 Views Asked by At

I'm trying to add two numbers in my application. One number is from database and other I want to insert in the application. It is very simple but I'm new to angularjs. This is my code:

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 }

All the app display is number 9 which is $scope.points as in my database it is 9 but it doesn't add the input number when I try to input any number. Anyone could please help?

2

There are 2 best solutions below

4
Stubbies On

I made a working demo based on your code and it seems to be working fine.

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>

0
Bartek On

That is the whole app.js the delete points functions are not finished yet

 // 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
}
});