Twitter bootstrap AngularJS UI引导typeahead和使用Coffeescript的ajax
我的问题很类似于 咖啡脚本:Twitter bootstrap AngularJS UI引导typeahead和使用Coffeescript的ajax,twitter-bootstrap,angularjs,coffeescript,angular-ui-bootstrap,Twitter Bootstrap,Angularjs,Coffeescript,Angular Ui Bootstrap,我的问题很类似于 咖啡脚本: $scope.tradingPartners = (searchOn) -> console.log("Searching on #{searchOn}") $.getJSON("../tp/tpLookupAdmin", {term: searchOn, max: 20}, (response)-> response) 生成Javascript: $scope.tradingPartners = function(
$scope.tradingPartners = (searchOn) ->
console.log("Searching on #{searchOn}")
$.getJSON("../tp/tpLookupAdmin", {term: searchOn, max: 20}, (response)->
response)
生成Javascript:
$scope.tradingPartners = function(searchOn) {
console.log("Searching on " + searchOn);
return $.getJSON("../tp/tpLookupAdmin", {
term: searchOn,
max: 20
}, function(response) {
return response;
});
};
使用它:
<input type="text" ng-model="testScript.sender" typeahead="sender as sender.label for sender in tradingPartners($viewValue)"
给出未定义的
工作的硬编码json示例:
[{"id":"1","label":"test1"},{"id":"2","label":"test2"}]
我错过了一些简单的事情
从kju答案编辑:
现在gen'd JS是
$scope.tradingPartners = function(searchOn) {
return $http.post("../tp/tpLookupAdmin?term=" + searchOn).then(function(response) {
return limitToFilter(response, 15);
});
};
但它仍然不起作用 你提到的问题已经有了你需要的所有答案,所以你的问题真的不是一个好问题 lookup函数必须返回数组或AngularJS样式的承诺。返回的是$.getJSON的返回值,这两个值都不是。代码中的回调函数将返回数组,但不返回任何位置。它不会以角度结束。这是没有帮助的,因为您正在这里发出异步HTTP请求。当请求返回时,查找函数早就返回了。因此,你需要回报一个承诺。AngularJS知道如何处理这个承诺并处理延迟的数据
正如我所说,另一个问题及其公认的答案已经包含了所有内容。摆脱$.getJSOn,使用Angular的$http服务,如图所示。您所引用的问题已经有了您需要的所有答案,因此您的问题确实不是一个好问题 lookup函数必须返回数组或AngularJS样式的承诺。返回的是$.getJSON的返回值,这两个值都不是。代码中的回调函数将返回数组,但不返回任何位置。它不会以角度结束。这是没有帮助的,因为您正在这里发出异步HTTP请求。当请求返回时,查找函数早就返回了。因此,你需要回报一个承诺。AngularJS知道如何处理这个承诺并处理延迟的数据
正如我所说,另一个问题及其公认的答案已经包含了所有内容。摆脱$.getJSOn,使用Angular的$http服务,如图所示。最后我使用了select2。无论如何,我认为这是一种更干净、更一致的方法
<input ui-select2="tpSearch" ng-model="testScript.sender" class="input-xlarge"/>
咖啡脚本:
$scope.tpSearch =
placeholder: "Type to search..."
minimumInputLength: 2
ajax:
url: "../tp/tpLookupPaged"
quietMillis: 100
data: (term, page) ->
term: term # query params
page: page
max: 10
results: (data, page) ->
more = (page * 10) < data.total
results: data.results, more: more
实现无限卷轴是轻而易举的事
确保您的JSON返回一个包含id和文本的数组,否则您将不得不为select2编写一个自定义格式化程序函数,不管怎样都非常简单最后我使用了select2。无论如何,我认为这是一种更干净、更一致的方法
<input ui-select2="tpSearch" ng-model="testScript.sender" class="input-xlarge"/>
咖啡脚本:
$scope.tpSearch =
placeholder: "Type to search..."
minimumInputLength: 2
ajax:
url: "../tp/tpLookupPaged"
quietMillis: 100
data: (term, page) ->
term: term # query params
page: page
max: 10
results: (data, page) ->
more = (page * 10) < data.total
results: data.results, more: more
实现无限卷轴是轻而易举的事
确保你的JSON返回一个包含id和文本的数组,否则你必须为select2编写一个自定义格式化程序函数,不管怎样都很简单谢谢你的回答,我做了一些更改,我认为JS看起来不错,但typeahead仍然不起作用…我会继续尝试。谢谢你的回答,我做了一些更改,我认为JS看起来不错,但是typeahead仍然不起作用……我们会继续尝试。