AngularJS - Store Data, Factory -


All in the afternoon,

We are trying to learn AngularJS for the last week and have a little guidance On this requirement, we want to be able to store data such as between user name, brand name and product name routes. At the moment we are using routes, but it is a pain that gets more and more to duplicate the code.

Apart from this, we have format data within section (3), they are a method that we can store this data inside the factory so that we can export the brand and updated products Can jump between? As soon as we leave each brand, it removes our input data.

Anybody can help if we would appreciate it very much, thanks.

Example

 On  amlProductCtrl = angular.module ('amlProductCtrl', []); // (1) employee amlProductCtrl.controller ('employee', ['$ scope', '$ http', function ($ radius, $ http) {$ http.get ('json / employees.json'). Function (data) {$ scope.employees = data;});}]); // (2) Brands amlProductCtrl.controller ('brand', ['$ scope', '$ routeParams', '$ http', function ($ scope, $ routeParams, $ http) {// username $ scope.employee = {"Username": $ routeParams.userName}; // welcome message $ scope.alerts = [{type: 'info', message: 'hello' + $ scope.employee.userName + '., Please enter a brand Select '}]; $ Scope.closeAlert = function {index} {$ scope.alerts.splice (index, 1);}; // JSON data get $ http.get (' json / brands.json '). Success (work (data) {$ Scope.brands = data;});}]); // (3) product amlProductCtrl.controller ('product', ['$ scope', '$ routeParams', '$ http', function ($ scope, $ routeParams, $ http) {// BrandName $ scope.brand = {"BrandName": $ routeParams.brandName}; // Username $ scope.employee = {"username": $ routeParams.userName}; // go to JSON data rpBrandName = $ routeParams.brandName; // brandName on rsBrandName = RpBrandName .replace (/ \ s + / g, ''); // remove space on lcBrandName = rsBrandName.toLowerCase (); // small on brandName = lcBrandName; $ http.get ('json /' + brandName + ' - products.json ') .success (function (data) {$ scope.products = data;}); // formdata $ scope.formData = {};}]);  

Here is a positive solution to create a data-sharing service between the controller This $ watch , So it may not be the most enthusiastic solution. I am also learning, so I can feel your pain: -)

  myApp.factory ('data service', function) {var myObject = {id: 0, name: 'set is not '}; Return {brandName: function () {myObject.name;}, set branded name: function (brand) {myObject.name = brand.name;}};});  

You can create your own purpose to better display your needs. Google has many articles that provide this solution, hoping that it will help. Check out this plunker: Go to # 2 and select a brand. You will see updating 2 items selected.


Comments