Angularjs 单击单选按钮时进行ajax调用,并使用角度响应更新模型值

Angularjs 单击单选按钮时进行ajax调用,并使用角度响应更新模型值,angularjs,Angularjs,我似乎不知道如何进行ajax调用并设置“$scope.ticker”模型,并以angular格式响应。基本上,这是一个ajax调用,以获取股票的最后价格(btc_usd、btc_eur或btc_rur) 这样“$scope.last”始终是选中的任何股票单选按钮的最后一个价格 <label><input type="radio" name="currency" value="btc_usd" ng-model="currency"> USD</label> &

我似乎不知道如何进行ajax调用并设置“$scope.ticker”模型,并以angular格式响应。基本上,这是一个ajax调用,以获取股票的最后价格(btc_usd、btc_eur或btc_rur)

这样“$scope.last”始终是选中的任何股票单选按钮的最后一个价格

<label><input type="radio" name="currency" value="btc_usd" ng-model="currency"> USD</label>
<label><input type="radio" name="currency" value="btc_eur" ng-model="currency"> EUR</label>
<label><input type="radio" name="currency" value="btc_rur" ng-model="currency"> RUR</label>


$scope.ticker = quote.ticker($scope.currency).then(function(data){
    var last = data.ticker.last;

    $log.log('last', last);
    $scope.ticker = last;
    $scope.last = last;
});

app.factory('quote', function($http, $log) {
    return {
        ticker: function(currency){
            $log.log('ticker.currency', currency);
            //return the promise directly.
            return $http.get('/api/last/'+currency)
                .then(function(result) {
                    //resolve the promise as the data
                    return result.data;
                });
        }
    };
});
USD
欧元
鲁尔
$scope.ticker=quote.ticker($scope.currency)。然后(函数(数据){
var last=data.ticker.last;
$log.log('last',last);
$scope.ticker=last;
$scope.last=last;
});
app.factory('quote',函数($http,$log){
返回{
股票代码:功能(货币){
$log.log('ticker.currency',currency);
//直接归还承诺。
返回$http.get('/api/last/'+货币)
.然后(函数(结果){
//将承诺解析为数据
返回结果数据;
});
}
};
});

首先,我要将
ng模型
更改为
ng模型=“$parent.currency”

第二,我们可以编写带有
ng repeat:

HTML:

现在从
$watch
可以基于
newValue


演示

可以在收音机上使用
ng change
,或
$scope.$watch('currency'
)来触发ajax调用我如何从$watch触发ajax调用?在$watch的回调范围内调用代码
 <label ng-repeat="value in list">
       <input type="radio" name="currency" ng-model="$parent.currency" ng-value="value.name" />{{value.name}}
      </label>
 $scope.list = [{
    name: "USD"
}, {
    name: "EUR"
}, {
    name: "RUR"
}];

$scope.currency = 'USD';

$scope.$watch(function () {
    return $scope.currency;
},

function (newValue, oldValue) {        
    console.log(newValue);
}, true);