【问题标题】:Passing data from View to Controller in Angularjs在Angularjs中将数据从视图传递到控制器
【发布时间】:2015-12-19 15:51:39
【问题描述】:

我从控制器中的 URL 获取数据,但我需要它动态

d3.json("http://localhost:2016/get_stats_for?brand_name="+$scope.brand,function(data))

我想从视图中的 textBox 获取 $scope.brand

我怎么能这样做?

【问题讨论】:

    标签: angularjs binding


    【解决方案1】:

    编辑 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 毫秒时才会发生更新(因此在快速键入的情况下,浏览器不会被多个获取请求限制)

    【讨论】:

    • 我有一个带有条件和值的 url,这个值需要从 view.html 中的文本框中设置
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    相关资源
    最近更新 更多