好吧,我對Angular相當陌生,但我陷入困境,這可能是一個相當簡單的解決方案。從ngModel更新JSON元素然後http POST
我有一個非常基本的Angular應用程序,它具有使用相同JSON文件填充數據的「Page View/Controller」和「Admin View/Controller」。
很明顯,我想在管理視圖中刪除一些元素,通過這個我的意思是,修改我的JSON文件。
繼承人我的一塊JSON文件的
{
"id": 22,
"title": "",
"section": "expositions",
"text": "2005 : « Ce soir la lune reve » (Casa de Francia et IFAL - Mexico DF)",
"url": "",
"fr": "",
"es": "",
"en": "",
"active": false
},
{
"id": 23,
"title": "",
"section": "expositions",
"text": "2006 : « Ce monde rayonnant » (Espace Triangle - Bruxelles, Belgique)",
"url": "",
"fr": "",
"es": "",
"en": "",
"active": false
}
我在我的控制範圍內的所有這些元素
$http.get('http://example.com/js/texts.json').then(function (result){
$scope.texts = result.data;
});
一切正常,我能填充我與ngModel視圖。
<ul class="angular-content">
<li ng-repeat="expo in texts | filter:{section: 'expositions'}">
<h4>
<input type="checkbox" ng-model="expo.active">
<textarea rows="2" cols="124" ng-model="expo.text"></textarea>
</h4>
<h4 ng-show="expo.fr"><a target="_blank" href="{{expo.fr}}">FR</a></h4>
<h4 ng-show="expo.es"><a target="_blank" href="{{expo.es}}">ESP</a></h4>
<h4 ng-show="expo.en"><a target="_blank" href="{{expo.en}}">ANG</a></h4>
</li>
</ul>
如果我堅持的是當我修改這些<textarea>
S的值,並嘗試更新我的JSON文件。
$scope.saveTexts = function() {
$scope.newTexts = JSON.stringify($scope.texts);
$http({
method: 'POST',
url: 'http://example.com/js/text.json',
data: $scope.newTexts,
contentType: 'application/json; charset=utf-8',
dataType: 'json'
});
}
我猜它的異步調用的問題,或者我透過JSON.stringify當我控制檯日誌的方式,我的變化將不會出現仍。
還當我嘗試添加元素無法正常工作:
如果我使用在NG-點擊此功能,我的觀點會在更新和添加這樣的元素,但不是我的$ scope.texts
$scope.addExpo = function(){
$scope.texts.push({
"id": 35,
"title": "TEST",
"section": "expositions",
"text": "TEST",
"url": "TEST ",
"fr": "",
"es": "",
"en": "",
"active": true
});
在此先感謝,對不起,很長的職位。
你可以使用'$ http'直接保存到文件中。您需要一臺可以接收「POST」內容並訪問底層文件系統的服務器。 – Claies
您是否擁有對後端服務器的控制權?如果是這樣,你在用什麼? –
你也不能發佈到json文件。它必須使用服務器端編程語言來完成,或者使用第三方存儲服務來處理您的數據。 – charlietfl