所以我有以下对象:
$scope.post = {user_id: $sessionStorage.user.user.id};
有了这个我有以下内容:
<textarea class="form-control" ng-model="post.text" style="overflow:scroll;height:150px;max-height:150px"></textarea>
在提交时,我想执行以下操作:
$scope.addPost = function () {
$http.post(api.getUrl('post', null),
{
post: $scope.post
}).success(function (response) {
$scope.post.id = response;
$scope.posts.push($scope.post);
});
$scope.post = {};
}
但是当我清除$scope.post
因为数据绑定时帖子是空的。所以我的问题是如何避免这种情况?
答案 0 :(得分:2)
您可以使用angular.copy()
制作副本,该副本将返回一个没有引用原始对象的新对象。
这也会删除角度作用域添加到对象的任何hashkeys,当服务器看到无法识别的键时,这可能会有问题
$scope.addPost = function () {
var postData = angular.copy($scope.post);
$http.post(api.getUrl('post', null),
{
post: postData
}).success(function (response) {
$scope.post.id = response;
$scope.posts.push(postData);
$scope.post = {}; //wait for success to clear
});
}
如前所述,应该等待成功清除实时版本
答案 1 :(得分:1)
这是因为Ajax调用的异步特性 - 您的$scope.post
比成功/错误回调更早执行。
你应该在回调中清除<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Photo Sites</title>
<style>
html, body, #map-canvas {
height: 100%;
margin: 0px;
padding: 0px
}
</style>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true"></script>
<script>
function initialize() {
var mapOptions = {
zoom: 8,
center: new google.maps.LatLng(-36.358548, 144.697350),
mapTypeId: google.maps.MapTypeId.HYBRID,
zoomControl: true,
zoomControlOptions: {
style: google.maps.ZoomControlStyle.LARGE
}
};
var map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
// Wrecking Train
// From here..
var contentString = '<div id="content">'+
'<div id="siteNotice">'+
'</div>'+
'<h1 id="firstHeading" class="firstHeading">Overhead Wrecking Train</h1>'+
'<div id="bodyContent">'+
'<p><b>The Overhead Wrecking Train</b> was photographed as it passed through Bunyip ' +
'in December 2012, dismantling the overhead equipment on the Gippsland line. The train '+
'is shown just west of the level crossing in Bunyip.'+
'<p>Photographs: '+
'<p><a href="http://www.hollywoodfoundry.com/photo%20collection/wrecking%20train%201.jpg">'+
'Wrecking Train 1</a> '+
'<p><a href="http://www.hollywoodfoundry.com/photo%20collection/wrecking%20train%202.jpg">'+
'Wrecking Train 2</a> '+
'<p><a href="http://www.hollywoodfoundry.com/photo%20collection/wrecking%20train%203.jpg">'+
'Wrecking Train 3</a> '+
'</p>'+
'</div>'+
'</div>';
var infowindow = new google.maps.InfoWindow({
content: contentString
});
var image = 'images/camera pointer.png';
var myLatLng = new google.maps.LatLng(-38.098461, 145.714411);
var HollyMarker = new google.maps.Marker({
position: myLatLng,
map: map,
icon: image,
title: 'Overhead Wrecking Train'
});
google.maps.event.addListener(HollyMarker, 'click', function() {
infowindow.open(map,HollyMarker);
});
// To here.
// Bendigo
// From here..
// Do not forget the variables that need a number after them below
var contentString1 = '<div id="content">'+
'<div id="siteNotice">'+
'</div>'+
'<h1 id="firstHeading" class="firstHeading">Bendigo Engine Shed</h1>'+
'<div id="bodyContent">'+
'<p><b>Bendigo Engine Shed Precincts</b> was photographed in April 2004 when the site was ' +
'under the control of Southern Shorthaul Railroad, and there were many odd vehicles in the '+
'yard and some interesting relics in the vicinity of the shed.'+
'<p>Photographs: '+
'<p><a href="http://www.hollywoodfoundry.com/photo%20collection/7RT on turntable.jpg">'+
'7RT on Turntable in Shed</a> '+
'<p><a href="http://www.hollywoodfoundry.com/photo%20collection/B class 2.jpg">'+
'B Class Shell</a> '+
'<p><a href="http://www.hollywoodfoundry.com/photo%20collection/B class bogie 1.jpg">'+
'B Class Bogie Frame</a> '+
'<p><a href="http://www.hollywoodfoundry.com/photo%20collection/B class bogies.jpg">'+
'B Class Bogie Frames</a> '+
'<p><a href="http://www.hollywoodfoundry.com/photo%20collection/D3 619.jpg">'+
'D3 619</a> '+
'<p><a href="http://www.hollywoodfoundry.com/photo%20collection/Ex SAR Blurbird.jpg">'+
'Ex SAR Bluebird</a> '+
'<p><a href="http://www.hollywoodfoundry.com/photo%20collection/GM 6 cylinder engine.jpg">'+
'GM 6 cylinder engine</a> '+
'<p><a href="http://www.hollywoodfoundry.com/photo%20collection/GM 8 cylinder engine.jpg">'+
'GM 8 cylinder engine</a> '+
'<p><a href="http://www.hollywoodfoundry.com/photo%20collection/GM 16 cylinder engine.jpg">'+
'GM 16 cylinder engine</a> '+
'<p><a href="http://www.hollywoodfoundry.com/photo%20collection/RT54 ex Portland.jpg">'+
'RT54 ex Portland</a> '+
'<p><a href="http://www.hollywoodfoundry.com/photo%20collection/RT50 Shunting Yard.jpg">'+
'RT50 Shunting the Yard</a> '+
'<p><a href="http://www.hollywoodfoundry.com/photo%20collection/H2 and H5.jpg">'+
'H2 and H5 in the Yard</a> '+
'</p>'+
'</div>'+
'</div>';
// Increment the variables below for each instance of this routine.
// The variables are: infowindow, myLatLng and Hollymarker
var infowindow1 = new google.maps.InfoWindow({
content: contentString1
});
var image = 'images/camera pointer.png';
var myLatLng1 = new google.maps.LatLng(-36.767630, 144.281140);
var HollyMarker1 = new google.maps.Marker({
position: myLatLng1,
map: map,
icon: image,
title: 'Bendigo Engine Shed Precincts'
});
google.maps.event.addListener(HollyMarker1, 'click', function() {
infowindow1.open(map,HollyMarker1);
});
// To here.
// Newport Workshops ARHS Museum Area
// From here..
// Do not forget the variables that need a number after them below
var contentString2 = '<div id="content">'+
'<div id="siteNotice">'+
'</div>'+
'<h1 id="firstHeading" class="firstHeading">ARHS Museum Early 60s</h1>'+
'<div id="bodyContent">'+
'<p><b>The ARHS Museum at Newport</b> was photographed in the early 60s before the area was enclosed. ' +
'At this time, the area consisted of little more than a track holding several locomotives'+
'<p>Photographs: '+
'<p><a href="http://www.hollywoodfoundry.com/photo%20collection/H220 Newport early 60s.jpg">'+
'H220 at ARHS Museum site at Newport in the early 60s</a> '+
'<p><a href="http://www.hollywoodfoundry.com/photo%20collection/X36 Newport early 60s.jpg">'+
'X36 at ARHS Newport, early 60s</a> '+
'<p><a href="http://www.hollywoodfoundry.com/photo%20collection/Locos Newport early 60s.jpg">'+
'Heritage Steam Locos at ARHS Newport, early 60s</a> '+
'</p>'+
'</div>'+
'</div>';
// Increment the variables below for each instance of this routine.
// The variables are: infowindow, myLatLng Hollymarker and contentString
var infowindow2 = new google.maps.InfoWindow({
content: contentString2
});
var image = 'images/camera pointer.png';
var myLatLng2 = new google.maps.LatLng(-37.853894, 144.883461);
var HollyMarker2 = new google.maps.Marker({
position: myLatLng2,
map: map,
icon: image,
title: 'ARHS Museum Early 60s'
});
google.maps.event.addListener(HollyMarker2, 'click', function() {
infowindow2.open(map,HollyMarker2);
});
// To here.
// Newport Workshops Storage Yard
// From here..
// Do not forget the variables that need a number after them below
var contentString3 = '<div id="content">'+
'<div id="siteNotice">'+
'</div>'+
'<h1 id="firstHeading" class="firstHeading">Newport Workshops in the early 60s.</h1>'+
'<div id="bodyContent">'+
'<p><b>The yards at Newport Workshops</b> had many interesting items stored there in the early 60s. ' +
'However film was an expensive commodity to me in those days, so these shots are a mystery as to '+
'why they were photographed while other items were not. As is often the case, I wish I had spent '+
'more film on shooting this amazing place.'+
'<p>Photographs: '+
'<p><a href="http://www.hollywoodfoundry.com/photo%20collection/7A on transporter at Newport Workshops early 60s.jpg">'+
'7A on transporter at Newport Workshops early 60s</a> '+
'<p><a href="http://www.hollywoodfoundry.com/photo%20collection/NB on wagon Newport early 60s.jpg">'+
'NB on wagon Newport early 60s</a> '+
'<p><a href="http://www.hollywoodfoundry.com/photo%20collection/NBCs on wagon at Newport.jpg">'+
'NBCs on wagon at Newport, early 60s</a> '+
'<p><a href="http://www.hollywoodfoundry.com/photo%20collection/Wooden wagon Newport early 60s.jpg">'+
'Wooden wagon at Newport early 60s</a> '+
'</p>'+
'</div>'+
'</div>';
// Increment the variables below for each instance of this routine.
// The variables are: infowindow, myLatLng and Hollymarker
var infowindow3 = new google.maps.InfoWindow({
content: contentString3
});
var image = 'images/camera pointer.png';
var myLatLng3 = new google.maps.LatLng( -37.847516, 144.881589);
var HollyMarker3 = new google.maps.Marker({
position: myLatLng3,
map: map,
icon: image,
title: 'Newport Workshops in the early 60s'
});
google.maps.event.addListener(HollyMarker3, 'click', function() {
infowindow3.open(map,HollyMarker3);
});
// To here.
// Dynon Locomotive Depot
// From here..
// Do not forget the variables that need a number after them below
var contentString4 = '<div id="content">'+
'<div id="siteNotice">'+
'</div>'+
'<h1 id="firstHeading" class="firstHeading">Dynon Locomotive Depot Early 60s</h1>'+
'<div id="bodyContent">'+
'<p><b>The Dynon Locomotive Depot</b> was quite easy to obtain access to in the early 1960 period, ' +
'not so much today. For some reason, this was the only shot I took inside, of B79 and W251.'+
'<p>Photographs: '+
'<p><a href="http://www.hollywoodfoundry.com/photo%20collection/Interior Dynon early 560s.jpg">'+
'Interior Dynon Locomotive Depot in the early 60s</a> '+
'</p>'+
'</div>'+
'</div>';
// Increment the variables below for each instance of this routine.
// The variables are: infowindow, myLatLng and Hollymarker
var infowindow4 = new google.maps.InfoWindow({
content: contentString4
});
var image = 'images/camera pointer.png';
var myLatLng4 = new google.maps.LatLng(-37.805982, 144.930593);
var HollyMarker4 = new google.maps.Marker({
position: myLatLng4,
map: map,
icon: image,
title: 'Dynon Locomotive Depot in the early 60s'
});
google.maps.event.addListener(HollyMarker4, 'click', function() {
infowindow4.open(map,HollyMarker4);
});
// To here.
// North Melbourne Locomotive Depot
// From here..
// Do not forget the variables that need a number after them below
var contentString5 = '<div id="content">'+
'<div id="siteNotice">'+
'</div>'+
'<h1 id="firstHeading" class="firstHeading">North Melbourne Locomotive Depot Early 60s</h1>'+
'<div id="bodyContent">'+
'<p><b>The North Melbourne Locomotive Depot</b> was a magical place, and yet again I failed to ' +
'photograph it thoroughly while it was around. Just one shot.'+
'<p>Photographs: '+
'<p><a href="http://www.hollywoodfoundry.com/photo%20collection/Interior North Melbourne early 60s.jpg">'+
'Interior North Melbourne Locomotive Depot in the early 60s</a> '+
'</p>'+
'</div>'+
'</div>';
// Increment the variables below for each instance of this routine.
// The variables are: infowindow, myLatLng and Hollymarker
var infowindow5 = new google.maps.InfoWindow({
content: contentString5
});
var image = 'images/camera pointer.png';
var myLatLng5 = new google.maps.LatLng(-37.808479, 144.940693);
var HollyMarker5 = new google.maps.Marker({
position: myLatLng5,
map: map,
icon: image,
title: 'North Melbourne Locomotive Depot in the early 60s'
});
google.maps.event.addListener(HollyMarker5, 'click', function() {
infowindow5.open(map,HollyMarker5);
});
// To here.
// Puffing Billy Belgrave
// From here..
// Do not forget the variables that need a number after them below
var contentString6 = '<div id="content">'+
'<div id="siteNotice">'+
'</div>'+
'<h1 id="firstHeading" class="firstHeading">Puffing Billy at Belgrave in the early 1960s</h1>'+
'<div id="bodyContent">'+
'<p><b>The Puffing Billy Station at Belgrave</b> was was relatively new at this time, and the small ' +
'engine shed adjacent to the station was the only locomotive accomodation. The kids playing on the track '+
'would be a no-no these days.'+
'<p>Photographs: '+
'<p><a href="http://www.hollywoodfoundry.com/photo%20collection/Puffing Billy at Belgrave looking West.jpg">'+
'Puffing Billy at Belgrave looking West</a> '+
'<p><a href="http://www.hollywoodfoundry.com/photo%20collection/Puffing Billy at Belgrave looking down and East.jpg">'+
'Puffing Billy at Belgrave looking down and East</a> '+
'</p>'+
'</div>'+
'</div>';
// Increment the variables below for each instance of this routine.
// The variables are: infowindow, myLatLng and Hollymarker
var infowindow6 = new google.maps.InfoWindow({
content: contentString6
});
var image = 'images/camera pointer.png';
var myLatLng6 = new google.maps.LatLng(-37.907570, 145.356438);
var HollyMarker6 = new google.maps.Marker({
position: myLatLng6,
map: map,
icon: image,
title: 'Puffing Billy Belgrave, 1960s'
});
google.maps.event.addListener(HollyMarker6, 'click', function() {
infowindow6.open(map,HollyMarker6);
});
// To here.
// Wonthaggi Line - Bridge Number 1
// From here..
// Do not forget the variables that need a number after them below
var contentString7 = '<div id="content">'+
'<div id="siteNotice">'+
'</div>'+
'<h1 id="firstHeading" class="firstHeading">Wonthaggi Line Bridge Number 1</h1>'+
'<div id="bodyContent">'+
'<p><b>Wonthaggi Line Bridge Number 1</b> at a distance of 3.9 Kms from Nyora Station ' +
'Watch this space!! '+
//'<p><a href="http://www.hollywoodfoundry.com/photo%20collection/Puffing Billy at Belgrave looking down and East.jpg">'+
//'Bridge 1</a> '+
'</p>'+
'</div>'+
'</div>';
// Increment the variables below for each instance of this routine.
// The variables are: infowindow, myLatLng and Hollymarker
var infowindow7 = new google.maps.InfoWindow({
content: contentString7
});
var image = 'images/camera pointer.png';
var myLatLng7 = new google.maps.LatLng(-38.355080, 145.688647);
var HollyMarker7 = new google.maps.Marker({
position: myLatLng7,
map: map,
icon: image,
title: 'Wonthaggi Line - Bridge Number 1'
});
google.maps.event.addListener(HollyMarker7, 'click', function() {
infowindow7.open(map,HollyMarker7);
});
// To here.
// Wonthaggi Line - Bridge Number 2
//
var contentString8 = '<div id="content">'+
'<div id="siteNotice">'+
'</div>'+
'<h1 id="firstHeading" class="firstHeading">Wonthaggi Line - Bridge Number 2</h1>'+
'<div id="bodyContent">'+
'<p><b>Wonthaggi Line - Bridge Number 2</b> is located 4.29 kms from Nyora Railway Station. It is a larger trestle bridge.'+
'</p>'+
'</div>'+
'</div>';
var infowindow8 = new google.maps.InfoWindow({
content: contentString8
});
var image = 'images/camera pointer.png';
var myLatLng8 = new google.maps.LatLng(-38.357978, 145.686209);
var HollyMarker8 = new google.maps.Marker({
position: myLatLng8,
map: map,
icon: image,
title: 'Wonthaggi Line - Bridge Number 2'
});
google.maps.event.addListener(HollyMarker8, 'click', function() {
infowindow8.open(map,HollyMarker8);
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="map-canvas"></div>
</body>
</html>
,而不是在外面。