Sono nuovo in Angular JS e sto imparando. Ho un div e caricare i dati da json all'avvio con controller con il seguente codice, ma voglio ricaricarlo di nuovo quando oggetto json cambiato dopo l'esecuzione di un'azione specifica.AngularJS: ricarica dati su div con controller e ng-repeat
index.html
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html ng-app="ezpf" xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="lib/angular.js"></script>
<script src="lib/jquery.min.js"></script>
</head>
<body onload="EZPF.Application.initialize()">
<div id="btn-import" class="my-button button-small" onClick="EZPF.Application.openFile(this)">
<span class="button-title">Import</span>
<span class="button-help">This button will do something else.</span>
</div>
<div class="page" id="pro" ng-controller="ProductsListCtrl as store">
<div ng-repeat="product in store.products.Products">
{{product.Title}}
</div>
</div>
</body>
</html>
myAngular.js
(function()
{
var app = angular.module('ezpf', []);
app.controller('ProductsListCtrl',['$scope', function($scope)
{
this.products = EZPF.Application.getProducts();
$scope.reload = function()
{
$scope.products = EZPF.Application.getProducts();
};
}]);
})();
Nel seguente file JavaScript apro il file JSON e ricaricare i prodotti oggetto con i nuovi dati. Dopo l'aggiornamento con il nuovo contenuto del file JSON devo ricaricare i dati. Ho provato a chiamare la ricarica dal controller ma non funziona. Per favore aiutami, grazie in anticipo.
application.js
var EZPF;
if (!EZPF)
EZPF = {};
if (!EZPF.Application)
EZPF.Application = {};
EZPF.Application =
{
products: [],
getProducts: function()
{
if (this.products.length == 0)
{
this.products =
{
"Products": [
{
"Title": "default name"
....
}]
}
}
return this.products;
},
openFile: function()
{
var docsDir = air.File.documentsDirectory;
try
{
var jsonFilter = new air.FileFilter("JSON Files", "*.json");
docsDir.browseForOpenMultiple("Select JSON Files", [jsonFilter]);
docsDir.addEventListener(air.FileListEvent.SELECT_MULTIPLE, filesSelected);
}
catch (error)
{
air.trace("Failed:", error.message)
}
function filesSelected(event)
{
air.trace(event.files[0].nativePath);
var myFile = new window.runtime.flash.filesystem.File();
var file = myFile.resolvePath(event.files[0].nativePath);
var fileStream = new air.FileStream();
fileStream.open(file, air.FileMode.READ);
this.products = fileStream.readMultiByte(fileStream.bytesAvailable, air.File.systemCharset);
fileStream.close();
air.trace(products);
$('#pro').reload();
}
}
};
E '' EZPF.Application.getProducts() 'restituisce qualcosa,' $ scope.products' viene aggiornato? – Zee
@Zee EZPF.Application.getProducts() restituisce lo stesso JSON con nuovi valori ma non so come richiamare il controller div che riempie {{product.Title}} –
Dopo tale riga scrivere '$ scope. $ Apply()' , il tuo div shud viene aggiornato. – Zee