将数据传递到范围

弗伦克

我正在尝试使用AngularJS创建实时搜索功能。我有一个输入字段:

<input type="text" placeholder="Search" data-ng-model="title" class="search">

它在那里传递在范围内的搜索关键字,所以我可以执行实时搜索(JS)并将结果直接显示给DOM

var app = angular.module("DB", []);

app.controller("Controller", function($scope, $http) {
  $scope.details = [],

  $http.defaults.headers.common["Accept"] = "application/json";
    $http.get('http://api.org/search?query=<need to pass search name here>&api_key=').

    success(function(data, status, headers, config) {

    }).
    error(function(data, status, headers, config) {
      //handle errors
  });
});
贾斯汀·约翰(Justin John)

在角度控制器内部使用监视表达式。

$scope.$watch('title', function (newValue, oldValue) {
  if(newValue != oldValue) {
    $http.get('http://api.org/search?query=' + newValue + '&api_key=')
         .success(function(data, status, headers, config) { /* Your Code */ })
         .error(function(data, status, headers, config) { /* Your Code */ });
  }
});

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章