我是AngularJs的新手,我有一个简单的问题。我想把这个名字。但是这个名字起初有一个价值,但在0.3-0.5秒之后它有其他价值。我不知道如何绑定它,价值可能会改变。AngularJs异步数据绑定
HTML代码:
<div ng-controller="FormController">
<h2 >
{{getName()}}<br>
<span class="icon-f-gear"></span>
<small>Procesas</small>
</h2>
</div>
角控制器:
var App = angular.module('form', ['ui.bootstrap']);
App.controller('FormController', ['$scope' ,'$http', function($scope, $http) {
$scope.tasks = [];
$scope.socket.stomp.subscribe("/app/tasks", function (message) {
if ($scope.tasks.length !=0){
$scope.tasks.removeAll();
}
JSON.parse(message.body).forEach(function (el) {
$scope.tasks.push(el);
});
});
$scope.getName = function(){
return $scope.tasks[0].form.name;
};
我使用STOMP协议,它给我的数据不会立即,因为它有发送请求到服务器,并把它找回来, 这需要时间。我试图在请求变量$ scope.tasks [0] .form.name从undefined更改为“Institution”后进行调试,但在屏幕上未定义停留,如何绑定变量以便更改该值。提前感谢您的帮助!
UPDATE 问题解决了,我只需要使用$ scope.apply(...)函数。代码在这里:
var App = angular.module('form', ['ui.bootstrap']);
App.controller('FormController', ['$scope' ,'$http', function($scope, $http) {
$scope.tasks = [];
$scope.socket.stomp.subscribe("/app/tasks", function (message) {
if ($scope.tasks.length !=0){
$scope.tasks.removeAll();
}
JSON.parse(message.body).forEach(function (el) {
$scope.tasks.push(el);
});
$scope.apply(function(){$scope.tasks});
});
$scope.getName = function(){
return $scope.tasks[0].form.name;
};
感谢您的帮助!
非常感谢您!我用这个$ scope.apply(...)函数,一切正常。虽然我只是在变量上使用它:$ scope。$ apply(function(){$ scope.tasks}; – Egizeris 2014-09-11 13:47:44