【发布时间】:2015-12-19 15:51:39
【问题描述】:
我从控制器中的 URL 获取数据,但我需要它动态
d3.json("http://localhost:2016/get_stats_for?brand_name="+$scope.brand,function(data))
我想从视图中的 textBox 获取 $scope.brand
我怎么能这样做?
【问题讨论】:
我从控制器中的 URL 获取数据,但我需要它动态
d3.json("http://localhost:2016/get_stats_for?brand_name="+$scope.brand,function(data))
我想从视图中的 textBox 获取 $scope.brand
我怎么能这样做?
【问题讨论】:
编辑 2
或者,您可以改用ng-change。
app.controller('MyController', function($scope, MyService){
$scope.brand = 'D&G'; //initialize value
$scope.onBrandChange = function(){
MyService.getByBrand($scope.brand).then(function(res){
var result = res.data; //here is your JSON
});
});
});
app.service('MyService', function($http){
this.getByBrand = function(brand){
var URL = "http://localhost:2016/get_stats_forbrand_name="+brand;
return $http.get(URL);
};
});
<div ng-controller='MyController'>
<input type='text' ng-change='onBrandChange()' ng-model-options="{debounce: 100}" ng-model='brand'></input>
</div>
您想从作用域跟踪变量的变化?
$scope.$watch('brand', function(newValue, oldValue){
//fetch the json file
});
将以下内容添加到视图上的 ng-model 元素
ng-model-options="{debounce: 100}"
仅当元素未更改超过 100 毫秒时才会发生更新(因此在快速键入的情况下,浏览器不会被多个获取请求限制)
【讨论】: