2016-11-27 64 views
0
var App = angular.module('myApp', []); 

App.controller('myCtrl', function($scope, $http) { 
    $http.get("http://api.com/&keywords=influence marketing") 
    .then(function(response) { 
     $scope.myData = response.data.influencers; 
    }); 
});  

所以我有上面的代碼加載角度正確的信息,但我希望它用一個新的關鍵字參數從一個搜索窗體重新加載。角度1搜索後內容更新

$("#search").submit(function(event) { 
    // Stop form from submitting normally 
    event.preventDefault(); 
    //$("#searchresults").empty(); 

    // Get some values from elements on the page: 
    var $form = $(this), 
    terms = $form.find("input[name='query']").val(), 
    url = "http://api.com/&keywords="; 


//output url+terms; 

    }); 
+1

你爲什麼混合jQuery和角?這將是更容易做到這一點在純角度 – maurycy

+0

什麼是最好的方式來做到這一點角? – Blynn

回答

0

這裏是一個簡單的方法來做到這一切都與角度。

你可以看到我使用的方式$ HTTP

PARAMS:$ scope.form,

,這將確保任何在form模式將作爲URL變量發送與GET的要求,所以你可以根據需要只要該模型將form.*將發佈添加儘可能多的輸入

http://plnkr.co/edit/14e5852qLzethvZLXHG8?p=preview

var app = angular.module('plunker', []); 
 

 
app.controller('MainCtrl', function($scope, $http) { 
 
    $scope.submitThisForm = function() { 
 
    
 
    $http({ 
 
     url: 'api', 
 
     params: $scope.form, 
 
     method: 'GET' 
 
    }).then(function onSuccess(response) { 
 
     //do some magic with response from server 
 
    }) 
 
    } 
 
    $scope.logToStackoverflow = function() { 
 
    console.log($scope.form) 
 
    } 
 
});
<!DOCTYPE html> 
 
<html ng-app="plunker"> 
 

 
<head> 
 
    <script data-require="[email protected]" src="https://code.angularjs.org/1.5.8/angular.js" data-semver="1.5.8"></script> 
 
</head> 
 

 
<body ng-controller="MainCtrl"> 
 
    <form name="myForm" ng-submit="submitThisForm()"> 
 
    <input type="text" ng-model="form.query"> 
 
    <br/> 
 
    <input type="text" ng-model="form.whatever"> 
 
    <br/> 
 
    <input type="submit" value="Submit form"> 
 
    <input ng-click="logToStackoverflow()" type="button" value="log output in stackoverflow" /> 
 
    </form> 
 
</body> 
 

 
</html>