angularjs - 如何在node.js "html page"中显示json stringify数组数据?

标签 angularjs json node.js ionic-framework clarifai

我有一个 JSON 数据数组,它是来自 Clarifai 识别的 predict(...) 调用的响应,我正在尝试在 HTML 中显示该数据数组。我使用 ng-repeat 来显示数组,但它不起作用。如何在 HTML 中显示数组?下面是我的 JS 文件和 HTML 文件的代码片段。

var app = angular.module("starter",['ionic']);
app.controller("myCtrl", function($scope,$rootScope) {
    const app = new Clarifai.App({ apiKey: 'e7f899ec90994aeeae109f6a8a1fafbe' });
   
    $rootScope.records = [];

    // predict the contents of an image by passing in a url
    app.models.predict("pets",'https://samples.clarifai.com/puppy.jpeg').then(
        function(response) {
            // The JSON.stringify() method converts a JavaScript value to a
            // JSON string optionally replacing values if a replacer function
            // is specified, or optionally including only the specified
            // properties if a replacer array is specified.
            for(var i=0; i < 2; i++) {
                $rootScope.records[i] = JSON.stringify(response.outputs[0].data.concepts[i].name);
                console.log(`module concept = ${$rootScope.records[i]}`);
                alert($rootScope.records[i]);
            }
        },
        function(err) {
            console.error(err);
        }
    ); 
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.min.js"></script>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <title></title>

    <link rel="manifest" href="manifest.json">

    <!-- un-comment this code to enable service worker
    <script>
        if ('serviceWorker' in navigator) {
            navigator.serviceWorker.register('service-worker.js')
                .then(() => console.log('service worker installed'))
                .catch(err => console.log('Error', err));
        }
    </script>-->

    <link href="lib/ionic/css/ionic.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">

    <!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
    <link href="css/ionic.app.css" rel="stylesheet">
    -->
    <script type="text/javascript" src="https://sdk.clarifai.com/js/clarifai-latest.js"></script>
    <!-- ionic/angularjs js -->
    <script src="lib/ionic/js/ionic.bundle.js"></script>

    <!-- cordova script (this will be a 404 during development) -->
    <script src="cordova.js"></script>

    <!-- your app's js -->
    <script src="js/app.js"></script>
</head>
<body ng-app="starter"ng-controller="myCtrl">
    <ion-pane>
        <ion-header-bar class="bar-stable">
            <h1 class="title">Ionic Blank Starter</h1>
        </ion-header-bar>
        <ion-content>
            <h1 ng-repeat="x in records">{{x}}</h1>
        </ion-content>
    </ion-pane>
</body>
</html>

最佳答案

我建议手动触发摘要周期,因为您的 predict 调用是异步的。

// add a reference to $timeout
app.controller("myCtrl", function($scope, $rootScope, $timeout) {
    // ...

    // predict the contents of an image by passing in a url
    app.models.predict("pets",'https://samples.clarifai.com/puppy.jpeg').then(
        function(response) {
            for(var i=0; i < 2; i++) {
                $rootScope.records[i] = JSON.stringify(response.outputs[0].data.concepts[i].name);
                console.log(`module concept = ${$rootScope.records[i]}`);
                // add the following $timeout call to manually trigger a digest cycle
                $timeout(() => {}); /* OR */ $scope.$digest();
            }
        },
        function(err) {
            console.error(err);
        }
    ); 
});

关于angularjs - 如何在node.js "html page"中显示json stringify数组数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46564252/

相关文章:

regex - 编写ng模式以禁止空格和特殊字符

json - 获取 SQL Server 2016 中 json 数组的长度

使用模式中转义字符的 JSON(模式)验证失败

javascript - 合并 JSON 对象中的键以使用 Javascript 返回合并键和值的数组

node.js - 在 mongoose 中创建数据时无法读取未定义的属性 'push'

javascript - Angular 中的 limitTo 过滤器是否会阻止在 ng-repeat 中加载其他项目?

javascript - 跟踪指令中的代码脚本

node.js - 在 sails 中调试时监听 EADDRINUSE 错误

javascript - 在 Mongoose 数据库中生成唯一的随机数而不重复?

javascript - 带有外部 map 着色的谷歌地图多边形