我有一个创建多个表单元素一个指令:指令模板内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