【问题标题】:Angular UI Bootstrap Typeahead template ng-src with funciton具有功能的 Angular UI Bootstrap Typeahead 模板 ng-src
【发布时间】:2014-08-06 23:03:08
【问题描述】:

我想知道如何在 Typeahead 的自定义模板的 ng-src 属性中使用一个函数。这是我的 html 模板:

<script type="text/ng-template" id="customTemplate.html">
    <a>
        <img ng-src="getWikiImgs({{match.model}})" width="16">
        <span bind-html-unsafe="match.label | typeaheadHighlight:query"></span>
    </a>
</script>
<div class="col-xs-10 center alt-txt-light">
    <span class="dash-pg-header-txt">Index an author!</span>
    <br/>
    <hr class="hr-separator"/>
    <br/>
    <div style="height: 1000px;">
        <h4>Search Wikipedia:</h4>
        <input type="text" ng-model="asyncSelected" placeholder="ie: John Bunyan" typeahead="item for item in getWikiResults($viewValue)" typeahead-wait-ms="500" typeahead-loading="loadingWikiResults" typeahead-template-url="customTemplate.html" class="form-control" />
        <br/>
        <i ng-show="loadingWikiResults" class="fa fa-refresh" style="text-align:left; float:left;"></i>
    </div>
</div>

所以在自定义模板脚本中,我尝试使用 ng-src 中的函数根据 Typeahead 使用的“match.model”变量从维基百科获取相应的图像。

这是控制器:

angular.module("app").controller("AuthorCreateController", function($scope, $state, 

$stateParams, $http) {

    $scope.getWikiResults = function($viewValue) {

        return $http.get('http://en.wikipedia.org/w/api.php?', {
            params: {
                srsearch: $viewValue,
                action: "query",
                list: "search",
                format: "json"
            }
        }).then(function($response){
            var items = [];
            angular.forEach($response.data.query.search, function(item){
                items.push(item.title);
            });
            return items;
        });
    };

    $scope.getWikiImgs = function(title) {

        $.getJSON("http://en.wikipedia.org/w/api.php?callback=?",
        {
            action: "query",
            titles: title,
            prop: "pageimages",
            format: "json",
            pithumbsize: "70"
        },
        function(data) {
            $.each(data.query.pages, function(i,item){
                return item.thumbnail.source;
            });
        });
    };


});

【问题讨论】:

  • 给你点意见,不知道为什么你的问题首先被标记了。

标签: javascript angularjs angular-ui-bootstrap angular-ui-typeahead


【解决方案1】:

问题是您的模板没有您合理预期的范围。

解决方案是(取决于模板出现的位置)在您的函数前面链接一些 $parent. 调用。

有关详细信息,请参阅 this git issuethis question

【讨论】:

    【解决方案2】:

    您的问题实际上是 不是 从 ng-src 调用函数。而是CORS [Cross-Resource-Origin-Sharing]

    这是您的代码的 Plunker: http://plnkr.co/edit/CvqhU9?p=preview

    例如,在输入中键入“a”,然后检查控制台,您会发现它确实成功地调用了该函数但是等待 2 秒,然后会出现以下内容:

    XMLHttpRequest cannot load http://en.wikipedia.org/w/api.php?&action=query&format=json&list=search&srsearch=a. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://run.plnkr.co' is therefore not allowed access.
    

    简而言之,当您在 www.foo.com 域上时,您不能从 www.bar.com 请求资源,除非 www.bar.com 启用了该资源。您可以在此处查看有关此问题的一些答案:XMLHttpRequest cannot load an URL with jQuery

    我希望这会有所帮助。

    【讨论】:

    • 我不认为您发布的 XHR 错误是“getWikiImgs()”函数的结果,因为它无法加载的 URL 没有匹配的参数 - 该 URL 对应于返回 Angular '$http.get()' 的 'getWikiResults()' 函数。顺便说一句,我确实遇到了 XHR 和 '$http.get()' 的问题,但我发现它似乎是一个 issue with Chrome 实际上导致了错误。不过,感谢您的回答。
    • 不客气。虽然我在 IE11 上尝试过它并给出了相同的结果。你在控制台中得到了什么吗?
    • 我认为 Plunker 有一些标头设置会导致 XHR 和 CORS 出现问题。我可以让它在我的应用程序中的 Chrome 本地工作(而不是在 Plunker 上)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-26
    • 1970-01-01
    • 2023-04-03
    相关资源
    最近更新 更多