如何识别字符串中的某些单词并返回结果? (AngularJS)

时间:2017-01-25 13:57:44

标签: javascript html angularjs ionic-framework google-cloud-platform

我正在尝试在识别来自Google Cloud API的某些字词后返回结果,例如有关它的信息。我只是在硬编码结果返回应用程序。

例如,使用谷歌云文本API,应用程序将返回Panadol框中的文本字符串。在字符串中,他们将识别单词“panadol”并显示结果,如描述。

这是我获得的google云API教程: https://www.sitepoint.com/image-recognition-with-the-google-vision-api-and-ionic/

index.html:

这将显示退货:

listCtrl.image_description是来自Google Cloud Api的字符串 listCtrl.identify_result是识别后的描述

<h3 class="text-center" ng-show="listCtrl.image_description"> {{ listCtrl.image_description }}</h3>
<h3 class="text-center" ng-show="listCtrl.identify_result"> {{ listCtrl.product_result }}</h3>

homecontroller.js:

res.responseses [0] [key] [0] .description是API返回的结果

 var key = me.detection_types[me.detection_type] + 'Annotations';
 me.image_description = res.responses[0][key][0].description;

这是我用于识别的代码,但它不起作用

function contains(str,text) {
   return str.indexOf(text) >= 0 ;  
   }
    if (contains(res.responses[0][key][0].description,'panadol')){
    me.product_word = 'Panadol is a drug';
    }  

App.js

nameApp.factory('Authorization', function() {

authorization = {};
authorization.image_description = '';
authorization.product_result = '';

return authorization;
});

nameApp.controller('listCtrl', function($scope,  Authorization) {
$scope.test = Authorization;  
});

任何有关制作功能的帮助都将深表感谢。谢谢!

当前错误:未定义res

2 个答案:

答案 0 :(得分:1)

解决了。

通过在响应之下的控制器内移动此代码来解决此问题。感谢大家的帮助:)

 function contains(str,text) {
 return str.indexOf(text) >= 0 ;  
 }
if (contains(me.image_description,'panadol')){
me.product_result = 'test';

} 

答案 1 :(得分:0)

您的包含函数中存在语法错误。

试试这个:

function contains(str,text) {
  return str.indexOf(text) >= 0;  
}