将$ http.get和$ scope中的值合并到2路数据绑定中

时间:2014-08-06 07:42:02

标签: javascript angularjs

我正在尝试让用户输入使用双向数据绑定更改范围上的值。我认为因为该值是在$ http成功函数内部计算的,所以它不允许进行双向数据绑定。有没有人知道允许$ scope.btc数量根据$ scope.usd输入动态更新的好解决方案?

控制器:

    $scope.usd = 0;

    $http({
        method:'GET' , 
        url: 'https://jsonp.nodejitsu.com/?url=https%3A%2F%2Fcoinbase.com%2Fapi%2Fv1%2Fcurrencies%2Fexchange_rates'
    }).
    success(function(data,status,headers,config){

        $scope.btc = parseFloat(data.btc_to_usd) * $scope.usd
    }).
    error(function(data,status,headers,config){

        console.log('failure')
    });

视图:

h1(class='pageTitle') Bitcoin Invoice
        label Amount USD:    
            input(type='text' ng-model='usd' style='margin-left:30px')
           br
         label Amount BTC:           
            input(type='text' ng-model='btc' style='margin-left:39px')
          br
          label Description:   
        input(type='text' ng-model='description' style='margin-left:3px')

1 个答案:

答案 0 :(得分:0)

请看这里:http://jsbin.com/dajus/1/只需更改你的$ http电话

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

app.controller('firstCtrl', function($scope, $http){
 $scope.usd = 10;


  $http.get('https://jsonp.nodejitsu.com/?url=https%3A%2F%2Fcoinbase.com%2Fapi%2Fv1%2Fcurrencies%2Fexchange_rates').
    then(function(response,status,headers,config){        


        $scope.btc = parseFloat(response.data.btc_to_usd) * $scope.usd;
    },
    function(data,status,headers,config){

        console.log('failure');
    });

});