【问题标题】:angularjs ionic app add two numbers one from database and other from inputangularjs ionic 应用程序添加两个数字,一个来自数据库,另一个来自输入
【发布时间】:2017-09-01 12:10:20
【问题描述】:

我正在尝试在我的应用程序中添加两个数字。一个数字来自数据库,另一个我想插入到应用程序中。这很简单,但我是 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,但是当我尝试输入任何数字时它不会添加输​​入数字。有人可以帮忙吗?

【问题讨论】:

    标签: javascript angularjs ionic-framework backand


    【解决方案1】:

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

    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>

    【讨论】:

    • 我不知道为什么在我的应用程序中工作。它仅在我这样做时才有效: {{points + inp}} 但我想将 $scope.sum 添加到数据库中以更新数据库中的点,这就是为什么这个解决方案不适合我。知道为什么它在我的应用程序中不起作用吗??
    • 你能展示更多你的控制器吗,包括你的作用域和数据库调用。
    • 您需要调用getMyPoints() 来设置数据库中的值。检查我编辑的答案。
    • 如果我这样做 $scope.points 将永远是 9?问题不在于 scope.points 我认为函数 sum 显示它问题在于 scope.inp 因为函数 sum 无法将其添加到 scope.points ?
    【解决方案2】:

    就是整个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
    }
    });
    

    【讨论】:

      猜你喜欢
      • 2015-03-01
      • 2020-08-21
      • 1970-01-01
      • 2012-01-31
      • 2021-02-26
      • 2019-05-28
      • 1970-01-01
      • 2019-09-09
      • 1970-01-01
      相关资源
      最近更新 更多