meteorangular度和请求 – 如何绑定?
我试图完成我的脚本的最后一个阶段,这是显示响应。 我有一个meteor的方法,通过基本authentication,从Zendesk的API请求一个对象。 此方法在我的客户端部分中由Meteor.call()调用。 我可以logging响应,但无法将其绑定和显示。
这是我的js:
if (Meteor.isClient) { // This code only runs on the client angular.module('equationKPIs', ['angular-meteor']); angular.module('equationKPIs').controller('EquationKPICtrl', ['$scope', function ($scope) { Meteor.call("zendeskUnsolvedTickets", function (error, results) { var json = JSON.parse(results); $scope.metrics = [{ 'name': 'Unsolved Tickets', 'value': json.view_count.pretty }]; console.log($scope.metrics); }); }]); } if (Meteor.isServer) { Meteor.methods({ zendeskUnsolvedTickets: function () { var request = Meteor.npmRequire('request'), username = '', password = '', url = 'https://equationconsulting.zendesk.com/api/v2/views/', auth = 'Basic ' + new Buffer(username + ':' + password).toString('base64'); var options = { method: 'GET', url: url, headers: { 'Authorization': auth } }; var tickets = Async.runSync(function (done) { options.url = url + '32396347/count.json'; request(options, function (error, response, body) { if (error) throw new Error(error); done(null, body); }); }); return tickets.result; } }); }
这是我的html:
<div class="container" ng-app="equationKPIs" ng-controller="EquationKPICtrl"> <div ng-repeat="metric in metrics"> <h4>{{metric.name}}</h4> <h2>{{metric.value}}</h2> </div> </div>
我已经尝试了一些js的迭代,比如:
$scope.metrics = Meteor.call("zendeskUnsolvedTickets", function (error, results) { var json = JSON.parse(results); var response = [{ 'name': 'Unsolved Tickets', 'value': json.view_count.pretty }]; return response; });
任何有关我在做什么错误的见解?
您需要调用$scope.$apply()
因为您在Angular的上下文之外分配$scope
(在ajaxcallback中)
Meteor.call("zendeskUnsolvedTickets", function (error, results) { var json = JSON.parse(results); $scope.metrics = [{ 'name': 'Unsolved Tickets', 'value': json.view_count.pretty }]; console.log($scope.metrics); $scope.$apply(); // this here });
更多在这里