angularjs - controller service rest calls -
i've been avoiding asking here there loads of examples out there have no idea what's i'm doing wrong. i'm trying put little angular app in place i'm struggling working pass basics.
i want little crud happening using reasonable clean structure use template other apps. i've found millions of examples using controllers , found millions of examples on how implement , di services. i'm trying put them no joy.
here go.
i have simple accounts.html
<h1>accounts</h1> <div ng-controller="accountcontroller"> <div> <table> <tr> <td>name</td> <td>description</td> <td>number</td> <td>accounttype</td> <td>delete</td> </tr> <tr ng-repeat="acct in accounts"> <td>{{acct.name}}</td> <td>{{acct.description}}</td> <td>{{acct.number}}</td> <td>{{acct.accounttype}}</td> <td>{{delete(acct)}}</td> </tr> </table> </div> <hr> <div> <div> name:<input type="text" ng-model="account.name"> </div> <div> description:<input type="text" ng-model="account.description"> </div> <div> number:<input type="text" ng-model="account.number"> </div> <div> account type:<select ng-model="account.accounttype"> <option ng-repeat="accttype in accounttypes">{{accttype}}</option> </select> </div> </div> <div> <input type="button" value="save" ng-click="save()"> <input type="button" value="cancel"> </div> </div> <!-- angular js --> <script src="assets/thirdparty/angular/1.4.8/js/angular.js"></script> <!-- app angular js --> <script src="assets/local/js/angular/accounts-app.js"></script> <script src="assets/local/js/angular/controller/accountcontroller.js"></script> <script src="assets/local/js/angular/service/refdataservice.js"></script> an accountcontroller.js
(function() { 'use strict'; var app = angular.module('accounts-app', ['account-service']); app.controller('accountcontroller', ['$scope', 'refdataservice', function($scope, refdataservice) { $scope.accounttypes = refdataservice.refdata.accounttypes; var account = { 'name' : '', 'description' : '', 'number' : '', 'accounttype' : '' }; $scope.account = account; $scope.accounts = []; $scope.save = function save() { $scope.accounts.push(this.account); $scope.account = { 'name' : '', 'description' : '', 'number' : '', 'accounttype' : '' }; }; }]); })(); and refdataservice.js
(function() { 'use strict'; var servicemodule = angular.module('account-service', []); servicemodule.service ('refdataservice', ['$http', function($http) { var url = 'http://localhost:8080/accounts-rest/api/refdata'; var refdata = {}; refdata.accounttypes = {}; refdata.accounttypes = function() { return $http.get(url + '/accounttypes'); } return refdata; }]); })(); i'm trying load account types rest , populate select dropdown. i've tried , changed make happen either console error or no error empty dropdown well.
ideally, in real application, refdataservice load, name says, reference data. advises on how improve welcome. :)
please let me know i'm doing wrong because i'm giving up. +_+
thanks in advance.
you're trying use accounttypes object/array:
$scope.accounttypes = refdataservice.refdata.accounttypes; ...but you've defined function in controller:
refdata.accounttypes = function() { return $http.get(url + '/accounttypes'); } additionally, looks you're expecting $http.get() return data directly - returns promise response of request argument. need use (assuming rest call returns object need):
$http.get(url + "accounttypes").then(function (response) { refdata.accounttypes = response; });
Comments
Post a Comment