我是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