我有一個創建多個表單元素一個指令:指令模板內AJAX查詢
// Directive
app.directive('comboInput', function(){
return {
scope: {
imodel: '=',
dmodel: '=',
bmodel: '=',
inputname: '@',
integers: '=',
decimals: '='
},
templateUrl: templatePath + 'combo-input.html'
}
});
// Template
<select id="" ng-model="imodel" ng-change="bmodel=imodel+dmodel" ng-options="value for value in integers"></select>
<select id="" ng-model="dmodel" ng-change="bmodel=imodel+dmodel" ng-options="(value|fraction) for value in decimals"></select>
<input type="number" name="{{inputname}}" ng-model="bmodel">
// Usage
<combo-input inputname="width" bmodel="width" imodel="widthI" dmodel="widthD" integers="ints" decimals="decs"></combo-input>
此應用程序的控制器具有搶到了價格的方法:
$scope.getProductPrice = function(){
return $http.post('/customize/angular/getProductPrice', {
sku: '$scope.sku',
width: $scope.width,
height: $scope.height
}).then(function(response){
$scope.productPrice = parseFloat(response.data).toFixed(2);
});
}
我用這個方法對各種零件通常通過在某個表單元素上調用ng-change="getProductPrice()"
;但是,從模板調用它時不起作用。
我已經嘗試了許多不同的答案,我在這裏找到了,但沒有一個按預期的方式工作(如無限$ http調用永遠運行),或者如果他們這樣做,提交答案的人提供沒有評論或其他細節爲什麼的代碼工作,所以我無法適應我的需求。最重要的是,各種方法都使用不同的技術,有些已被棄用,所以沒有一致性來確定我需要做什麼。
如何從我的指令模板中撥打getProductPrice()
?
編輯
我創建了一個Plunker證明舊值傳遞給getProductPrice()
方法。
http://plnkr.co/edit/1nRs26nTaSOztjvVyLYg?p=preview
我原以爲這應該是這麼簡單,我不明白爲什麼這麼多其他答案讓一切變得如此複雜;然而,在這個例子中,請求參數總是舊值而不是新值。 – pspahn
究竟是什麼請求參數?它是''$ scope.sku「'?這在你的例子中似乎是一個常量字符串。 – floribon
PS:你發現的複雜答案可能是直接綁定'$ http'調用給諸如「{{getProductPrice()}}」的觀察者(因此在每個摘要循環中執行)。這曾經被支持,但不再是通話正在進行(並且正在殺死服務器)。在你的情況下,你只在特定的事件('ng-change')執行調用,所以沒關係。 – floribon