I tought angular and faced with a problem: I have a SPA which contains two parts with controllers, the data returns from json file. The first conroller for showing the menu, the second - for adding a new item. For now in json file there are two object, but when I added a third item in the second controller, It dissapeared when I return on the first page. How can I fix it? I've read that factory can transfer the data between controllers, but I've never use It.
Angular module:
var myApp = angular.module("testModule", ['ngRoute']);
myApp.config(function ($routeProvider){
$routeProvider.when('/', {
templateUrl: 'pages/main.html',
controller: 'mainCtrl'
})
.when('/add', {
templateUrl: 'pages/add.html',
controller: 'addCtrl'
})
})
myApp.controller("mainCtrl", function ($scope, $http) {
$http.get("model/data.json").success(function (response) {
$scope.list = response;
})
});
myApp.controller("addCtrl", function ($scope, $http) {
$scope.addAdv = function(newAdv){
$http.get("model/data.json").success(function (response) {
response.push({
name: newAdv.name,
shop: newAdv.shop
});
})
};
});
JSON file:
[{
"name": "New Item",
"shop": "Titan"
},
{
"name": "New Item1",
"shop": "Titan"
}]
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…