视频的更改不会反映在ng-click的事件中

时间:2016-02-04 04:49:51

标签: javascript angularjs ionic-framework

所以我有这个HTML:

<body ng-app="calculator" ng-controller="calculatorCtrl">
      <input type="text" ng-model="num1" />
      <input type="text" ng-model="num2" />
      <button ng-click="add()">Add</button>
      <label>{{result}}</label>
</body>

和这个JS:

angular.module('calculator', [])

.controller('calculatorCtrl', function($scope){
    $scope.num1 = 0;
    $scope.num2 = 0;
    $scope.result = 0;
    $scope.add = function(){
        console.log($scope.num1);
        console.log($scope.num2);
        $scope.result = parseInt($scope.num1) + parseInt($scope.num2);
        console.log($scope.result);
    };
});

在我的控制器中,我有3个变量,num1num2result。另外,我有一个名为add的函数。该函数应将result值更改为num1+num2

我使用ng-click属性将函数绑定到按钮单击事件。 我还将num1num2result绑定到各自的组件。

但是,我发现num1num2的值始终为零。以下是console.log的结果:

0
0
0

我知道我可以将HTML更改为:

<body ng-app="calculator" ng-controller="calculatorCtrl">
      <input type="text" ng-model="num1" />
      <input type="text" ng-model="num2" />
      <label>{{parseInt(num1)+parseInt(num2)}}</label>
</body>

它会完美运作。

但我仍然感到困惑,为什么第一种方法不起作用?

编辑:这似乎与离子框架有关。我删除了不相关的代码,以便让每个人都专注于我认为问题所在的位置。显然我错了。所以这里再次是工作代码和不工作的代码:

这不起作用:

angular.module('calculator', ['ionic'])

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    if(window.cordova && window.cordova.plugins.Keyboard) {
      // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
      // for form inputs)
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);

      // Don't remove this line unless you know what you are doing. It stops the viewport
      // from snapping when text inputs are focused. Ionic handles this internally for
      // a much nicer keyboard experience.
      cordova.plugins.Keyboard.disableScroll(true);
    }
    if(window.StatusBar) {
      StatusBar.styleDefault();
    }
  });
})

.controller('calculatorCtrl', function($scope){
    $scope.num1 = 0;
    $scope.num2 = 0;
    $scope.result = 0;
    $scope.add = function(){
        console.log($scope.num1);
        console.log($scope.num2);
        $scope.result = parseInt($scope.num1) + parseInt($scope.num2);
        console.log($scope.result);
    };
});

这有效:

angular.module('calculator', [])
.controller('calculatorCtrl', function($scope){
    $scope.num1 = 0;
    $scope.num2 = 0;
    $scope.result = 0;
    $scope.add = function(){
        console.log($scope.num1);
        console.log($scope.num2);
        $scope.result = parseInt($scope.num1) + parseInt($scope.num2);
        console.log($scope.result);
    };
});

现在的问题是:离子有什么问题?

再次更新

我不确切地知道,但是如果我将变量作为对象属性,它似乎会起作用。这与离子完美配合:

的index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <title></title>

    <link href="lib/ionic/css/ionic.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">

    <!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
    <link href="css/ionic.app.css" rel="stylesheet">
    -->

    <!-- ionic/angularjs js -->
    <script src="lib/ionic/js/ionic.bundle.js"></script>

    <!-- cordova script (this will be a 404 during development) -->
    <script src="cordova.js"></script>

    <!-- your app's js -->
    <script src="js/app.js"></script>
  </head>
  <body ng-app="calculator" ng-controller="calculatorCtrl">

    <ion-pane>
      <ion-header-bar class="bar-stable">
        <h1 class="title">Calculator</h1>
      </ion-header-bar>
      <ion-content>
          <label class="item item-input">
              <input type="text" ng-model="data.num1" />
          </label>
          <label class="item item-input">
              <input type="text" ng-model="data.num2" />
          </label>
          <button class="button button-block button-positive" ng-click="add();">Add</button>
          <label>{{data.result}}</label>
      </ion-content>
    </ion-pane>
  </body>
</html>

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('calculator', ['ionic'])

.run(function($ionicPlatform) {
    $ionicPlatform.ready(function() {
        if(window.cordova && window.cordova.plugins.Keyboard) {
            // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
            // for form inputs)
            cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);

            // Don't remove this line unless you know what you are doing. It stops the viewport
            // from snapping when text inputs are focused. Ionic handles this internally for
            // a much nicer keyboard experience.
            cordova.plugins.Keyboard.disableScroll(true);
        }
        if(window.StatusBar) {
            StatusBar.styleDefault();
        }
    });
})

.controller('calculatorCtrl', function($scope){
    $scope.data = {
        num1 : 0,
        num2 : 0,
        result : 0,
    }
    $scope.add = function(){
        $scope.data.result = parseInt($scope.data.num1) + parseInt($scope.data.num2);
    }
});

新问题:

为什么这个有效,而旧有无效?

0 个答案:

没有答案