【问题标题】:ng-show is not producing expected output after replace in Angularng-show 在 Angular 中替换后没有产生预期的输出
【发布时间】:2016-12-23 19:25:23
【问题描述】:

如果有人问过类似的问题,请原谅我。我正在使用 Angular JS 1.5 制作一个网络应用程序。

在下面显示的控制器中,我请求了一个 JSON 对象数组并将它们存储在 $scope.gameList 中。对于上下文,我有一个 JSON 文件,其中包含存储为对象数组的 Steam 商店中的游戏列表。然后,我使用此代码通读,如果名称匹配,则将其添加到名为 cleanGameNames 的数组中,然后将其存储在 $scope.cleanedGameNames 中。 Data 是一个 JSON 对象数组,其中包含来自 IGDB API 的搜索结果列表。

if(data[i]["name"].replace(": ", " - ") === $scope.gameList[j]["name"]){

            console.log("replaces colon trailing space with dash");
            cleanedGameNames[i] = $scope.gameList[j]["name"];
            console.log(cleanedGameNames[i]);
}

在我的 HTML 中,我正在循环访问 $scope.gameList(一个包含所有 Steam 游戏的数组)和 $scope.games(一个包含来自 IGDB API 的搜索结果列表的数组)。

<ul ng-repeat="steam in gameList | filter:game.name:true " ng-show="$first">
    <div ng-repeat="gameName in cleanedGameNames track by $index">            
        <li>
            <div ng-show="gameName == steam.name">
                <p ng-bind="steam.name">{{steam.name}}</p>
                <a ng-href="http://store.steampowered.com/app/{{steam.appid}}">Buy on steam</a>
            </div>
        </li>
    </div>
</ul>

我期望它在我的表格单元格中显示的是

Warlock - Master of the Arcane (this was originally Warlock: Master of the Arcane but I did a replace on this string and stored the result of the replace function in an element of my cleanedGameNames array. 
Buy on Steam //This would be a link to buy the game on the steam store

相反,当我尝试在表格中显示此字符串时,我什么也得不到。这一切都显示在 HTML 表格中。在我的 angular.js 文件中,我已经完成了一些 console.log 语句,似乎术士 - 奥术大师正按预期发送到 $scope.cleanedGameNames(我在我的 HTML 代码中循环的数组)。

有什么我遗漏的吗?

【问题讨论】:

    标签: javascript angularjs arrays json


    【解决方案1】:
    1. 尝试在 html 中查看 gameList:添加 {{gameList}} 如果你看到了,那么
    2. 删除 |过滤器:游戏名称:真

    这是多余的 {{steam.name}}

    删除 {{steam.name}}

    【讨论】:

    • 感谢您的快速回复。我很乐意尝试一下。我删除了过滤器,然后它导致网站/网络应用程序变慢了很多。
    • 我应该提到的另一件事是,在另一个搜索中,它按预期显示了“使命召唤:现代战争 3”和“在 Steam 上购买”。
    【解决方案2】:

    由于没有人回答(并且已经有一段时间没有人回复对此问题的评论了),我想分享一下我是如何解决这个问题的。

    在我的循环中,我没有将匹配的字符串从 $scope.gameList[j]["name"] 写入到cleanedGameNames,而是简单地决定用原来的内容覆盖 data[i]["name"] 中的内容在 $scope.gameList[j]["name"].

    感谢所有人对此提供的帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多