在angularjs中共享控制器中的数据

时间:2015-07-04 05:05:18

标签: angularjs angularjs-scope

我想与两个控制器共享数据,但我的应用程序有一个错误。当我第一次点击 x2 时,它会返回NAN,但第二次它会正常工作。

我试图解决this question



 var mainApp = angular.module("mainApp", []);
 mainApp.service('MathService', function() {

   this.multiply = function(a, b) {
     return a * b
   }
 });

 mainApp.service('CalcService', function(MathService) {
   this.square = function(a) {
     return MathService.multiply(a, a);
   }
 });

 mainApp.service('Data', function() {
   return {
     result: ''
   };
 });

 mainApp.controller('CalcController', function($scope, CalcService, Data) {
   $scope.square = function() {
     $scope.Data = Data;
     $scope.result = CalcService.square(Data.number);
   }
 });

 mainApp.controller('CalcController2', function($scope, MathService, Data) {
   $scope.multiply = function() {
     $scope.Data = Data;
     $scope.result2 = MathService.multiply(Data.number, $scope.number2);
   }
 });

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.21/angular.min.js"></script>

<body>
  <h2>AngularJS Sample Application</h2>
  <div ng-app="mainApp">
    <div ng-controller="CalcController2">
      <p>
        Enter a number:
        <input type="number" ng-model="number2">
        <button ng-click="multiply()">multiply</button>
        <p>Result x*y: {{result2}}</p>

        <div ng-controller="CalcController">
          <button ng-click="square()">X<sup>2</sup></button>

          <p>Result x2:{{result}}</p>
          <br>
          <p>
            Enter a number:
            <input type="number" ng-model="Data.number">
        </div>
    </div>
  </div>
</body>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

代码运行正常。原因是你将第二个输入留空,因此整个乘法中断。 请结帐。

var mainApp = angular.module("mainApp", []);
mainApp.service('MathService', function() {

  this.multiply = function(a, b) {
    return a * b
  }

});

mainApp.service('CalcService', function(MathService) {
  this.square = function(a) {
    return MathService.multiply(a, a);
  }
});
mainApp.service('Data', function() {
  return {
    result: ''
  };
});

mainApp.controller('CalcController', function($scope, CalcService, Data) {
  $scope.square = function() {
    debugger;
      Data=$scope.Data;
    $scope.Data = Data;
    $scope.result = CalcService.square(Data.number);

  }
});
mainApp.controller('CalcController2', function($scope, MathService, Data) {

$scope.Data = {number:1};
  $scope.multiply = function() {
      debugger;
    Data=$scope.Data;
    
    $scope.Data = Data;
    $scope.result2 = MathService.multiply(Data.number, $scope.number2);

  }
   
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.21/angular.min.js"></script>

<body>
  <h2>AngularJS Sample Application</h2>
  <div ng-app="mainApp">



    <div ng-controller="CalcController2">
      <p>Enter a number:
        <input type="number" ng-model="number2">
        <button ng-click="multiply()">multiply</button>
        <p>Result x*y: {{result2}}</p>
        <div ng-controller="CalcController">

          <button ng-click="square()">X<sup>2</sup>
          </button>
          <p>Result x2:{{result}}</p>
          <br>
          <p>Enter a number:
            <input type="number" value="0" ng-model="Data.number">
        </div>

    </div>



  </div>

</body>