angularJs

时间:2015-05-28 18:29:44

标签: angularjs service watch

我有一个来自控制器和两个输入框的值。 每当我在一个输入框中输入任何值时,我都需要它。从控制器和输入框检索的值的差异使用angularJs显示在另一个输入框中。 例如:- {{sum}} - >是我从控制器获得的值。

<input  type="number" ng-model="firstnumber" />
<input type="number" ng-model="secondnumber"/>

我尝试的是设置和获取总和值的服务,并在每次更改值时观察更改值。

我的服务是: -

angular.module('myapp').service("cmModalService", function($scope){
var sum= ={};
    getSum = function(){
        return sum;
    }

    setSum = function(value){
        sum=value;
    };
});

在我定义的控制器中

$scope.$watch('firstnumber', function(newValue, oldValue) { 
var sum=cmModalService.getSum();    
if(newValue!=null)
{ secondnumber = sum -firstnumber;
}
});

$scope.$watch('secondnumber', function(newValue, oldValue) { 
var sum=cmModalService.getSum();    
if(newValue!=null)
{ firstnumber = sum -secondnumber;
}
});

但每当我在输入框中更改值时,流程永远不会进入监视方法,并且值不会发生变化。 还有其他方法可以达到这个目的吗?

我也尝试过使用ng-change,但仍然无法得到确切的结果。

在控制器内部,我已将更改方法定义为

$scope.changefirstnumber=function(firstnumber, sum){
   $scope.secondnumber = sum- firstnumber;
   };


   $scope.changesecondnumber=function(secondnumber, sum){
   $scope.firstnumber= sum- secondnumber;
   }; 

和html     

[Plunker link]

2 个答案:

答案 0 :(得分:0)

您没有设置范围变量。试试这个。

$scope.$watch('firstnumber', function(newValue, oldValue) { 
var sum=cmModalService.getSum();    
if(newValue!=null)
{ $scope.secondnumber = sum -$scope.firstnumber;
}
});

$scope.$watch('secondnumber', function(newValue, oldValue) { 
var sum=cmModalService.getSum();    
if(newValue!=null)
{ $scope.firstnumber = sum - $scope.secondnumber;
}
});

Working Plunkr

答案 1 :(得分:0)

修改 根据您提供的一些新信息,这是您之后的信息吗? http://jsfiddle.net/37gv1kbe/

var app = angular.module('myApp',[]);

app.controller('MyController', function($scope,cmModalService)
{
    $scope.firstnumber = 5;
    $scope.secondnumber = 10;

    $scope.$watch('firstnumber', function()
    {
        $scope.total = cmModalService.getSum() - $scope.firstnumber
    });

    $scope.$watch('secondnumber', function()
    {
        $scope.total = cmModalService.getSum() - $scope.secondnumber;
    });
});

app.controller('MySecondController', function($scope,cmModalService)
{
    $scope.rand = Math.round(Math.random() * 100);  
    cmModalService.setSum($scope.rand);
});

app.service('cmModalService', function()
{
    var sum;
    return {
        getSum: function()
        {
            return sum;
        },    
        setSum: function(value)
        {
            sum = value;
        }    
    }   
});

原始回答

关于我的评论,如果您需要访问控制器中的总计,则可以保存firstnumbersecondnumber的val,如此

http://jsfiddle.net/pvqm4tcw/

var app = angular.module('myApp',[]);

app.controller('MyController', function($scope)
{
    $scope.firstnumber = 5;
    $scope.secondnumber = 10;

    $scope.$watch('firstnumber', function()
    {
        $scope.total = $scope.firstnumber + $scope.secondnumber;
    });

    $scope.$watch('secondnumber', function()
    {
        $scope.total = $scope.firstnumber + $scope.secondnumber;
    });
});

HTML:

<body ng-app="myApp">
    <div ng-controller="MyController">
        <input  type="number" ng-model="firstnumber" />
        <br>
        <input type="number" ng-model="secondnumber"/>
        <br>
        {{total}}
    </div>
</body>

如果你正在使用Angular 1.3+,他们会有一个$watchGroup,可以使代码更小

var app = angular.module('myApp',[]);

app.controller('MyController', function($scope)
{
    $scope.firstnumber = 5;
    $scope.secondnumber = 10;

    $scope.$watchGroup(['firstnumber','secondnumber'], function()
    {
        $scope.total = $scope.firstnumber + $scope.secondnumber;
    });
});