如果根據我的功能重新加載數據,則視圖不會更改。我發現,應該通過加入$scope.$apply()
來解決。更新範圍後的某處,但在這種情況下,我收到此錯誤。
https://docs.angularjs.org/error/ $ rootScope/inprog P0 = $申請
我的腳本:
var roomsApp = angular.module('roomsApp', []);
roomsApp.controller('RoomListController', function RoomListController($scope, $http) {
$scope.refreshData = function() {
$http({
method: 'GET',
url: '<?=url('getRooms')?>'
}).then(function successCallback(response) {
$scope.rooms = response.data;
console.log(response);
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
}
$scope.refreshData();
$scope.addRoom = function (title) {
if (title != "" && typeof title !== 'undefined') {
$http({
method: 'POST',
url: '<?=url('addRoom')?>',
data: {
title: title
}
}).then(function successCallback(response) {
$scope.refreshData();
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
}
$scope.$apply();
};
});
我的角度 - HTML:
<div class="row" ng-app="roomsApp">
<div class="col-md-8">
<div class="panel panel-default" ng-controller="RoomListController">
<div class="panel-heading">Rooms</div>
<div class="panel-body">
<ul class='list-group' ng-repeat="room in rooms">
<li class="list-group-item">{{room.title}}</li>
</ul>
</div>
</div>
</div>
<div class="col-md-4">
<div class="panel panel-default" ng-controller="RoomListController">
<div class="panel-heading">New room</div>
<div class="panel-body">
<div class="input-group input-group-lg">
<span class="input-group-addon" id="title">Title</span>
<input type="text" ng-model="title" class="form-control" placeholder="" aria-describedby="title">
</div>
<br>
<button ng-click="addRoom(title);" type="button" class="btn btn-primary pull-right"><span
class="glyphicon glyphicon-plus"></span></button>
</div>
</div>
</div>
</div>
您的發佈方法是否有效? –
什麼是<?= url('getRooms')?>? –
看起來像一些PHP ^^ – owca