Ellipse Ellipse - 1 year ago 72
AngularJS Question

I cannot assign my result of my api request to the view in AngularJS

This is only a portion of my code. For some reason I can assign vm.print to my response, it does not show. However, when I console.log() the response, it appears in the console. Please help. I can assign vm.print to any string outside of the app.model.predict(){} function but when I assign it inside the function, it does not show up in the view , but only shows up in the console.

<main class="app container" ng-controller="webcamController as vm">
<div class="row">
<div class="col-md-12" style="text-align: center">
<ng-webcam config="vm.config"
on-capture-progress="vm.onCaptureProgress(src, progress)"
<div class="col-md-12">
<p id="progress">Progress: {{vm.progress}}%</p>
<div class="col-md-12 buttons" ng-if="vm.showButtons">
<button class="btn btn-primary" type="button" ng-disabled="!vm.captureButtonEnable" ng-click="vm.capture()">Capture</button>
<button class="btn btn-primary" type="button" ng-click="vm.off()">Camera off</button>
<button class="btn btn-primary" type="button" ng-click="vm.on()">Camera On</button>
<div id="result" class="row"></div>
<!-- Displays analysis -->
<!--<div ng-repeat="x in analysis">
<h3>Example heading <span class="label label-default">{{x.name}}</span></h3>
<div class="text-center">{{vm.print}}</div>



vm.test = function(src) {

//converts to string
var uri = src.toString();
//String manipulation
uri = uri.slice(23, uri.length);

var app = new Clarifai.App(

//This message will show up
vm.print = "Default message";

// predict the contents of an image by passing in a url
app.models.predict(Clarifai.GENERAL_MODEL, {base64: uri}).then(
function(response) {

//str returns a string
var str = response.request.response;
//converting string to json
var json = JSON.parse(str);
//returns result
//this does not show up in the view
vm.print = json.outputs[0].data.concepts[0].name;
function(err) {
vm.print = "Opps, something went wrong :( " + err;



This is only a portion of my code. It will not run in the console. I just need to someone to look at the syntax who is more familiar with angularjs and the clarifai api. thanks.

Answer Source

I'm not familiar with the library that you're using when you execute Clarifai.App, but I'm guessing it's not an Angular lib, so it probably just doesn't run the digest cycle automatically for you in the promise resolve (.then() callback)

You have to do it yourself. Try to inject thr $scope service, and run the $scope.$apply() method, like this:

app.controller('webcamController', function ($scope) {
    /* ... */
    app.models.predict(Clarifai.GENERAL_MODEL, {base64: uri}).then(
        function(response) {
            /* ... */
            vm.print = json.outputs[0].data.concepts[0].name;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download