angularjs - My service is returning the function's text and not an object -


i have service share object in app... want post object mongo db when call function should return object gives me function's text.

the service here:

angular.module('comhubapp')   .service('markerservice', function () {   this.markers = [];     this.newmarker = {  title: '',         description: '',         lat: '',         lon: '',         user: '',         created_at: '' }; // supposed return marker object     this.newmarker = function  () {         return this.newmarker;     };     this.settitle = function (title) {         this.newmarker.title = title;         console.log('title service set: ' + title);     };     this.setdescription = function (description) {         this.newmarker.description = description;         console.log('description service set: ' + description);     };     this.setlat = function (lat) {         this.newmarker.lat = lat;         console.log('lat service set: ' + lat);     };     this.setlon = function (lon) {         this.newmarker.lon = lon;         console.log('lon service set: ' + lon);     };     this.reset = function () {         this.newmarker =  { title: '',             description: '',             lat: '',             lon: '',             user: '',             created_at: ''};             }      this.setmarkers = function (markers) {         this.markers = markers;     }     this.markers = function () {         return this.markers;     }     this.addmarker = function (marker) {         //todo append marker     } }); 

newmarker returns:

this.newmarker = function  () {         return this.newmarker;     }; 

the controller using service here

$scope.addmarker = function() { if($scope.newmarker.title === '') {     console.log('newmarker title empty');   return; }  markerservice.settitle($scope.newmarker.title); markerservice.setdescription($scope.newmarker.description);  console.log(markerservice.newmarker()); // $http.post('/api/markers', { name: $scope.newmarker }); // $scope.newmarker = ''; }; 

$scope new marker form data.. tried put right service no success. instead out form data controller push service. if there better way please let me know.

if service bad in other way let me know new , followed answer saw on here.

you overriding object function. give them different names , should work fine.

this.newmarker = { ... }; this.getnewmarker = function () { return this.newmarker }; 

edit:

you should create new instance marker. otherwise edit same object time. here example made. not best practice hope point.

angular.module('serviceapp', []) .factory('marker', function () {     function marker() {         this.title = '';         this.descrpition = '';     }      // use setters , getters if  want make variable private     // in example not using these functions     marker.prototype.settitle = function (title) {         this.title = title;     };      marker.prototype.setdescription = function (description) {         this.description = description;         };      return marker; }) .service('markerservice', function (marker) {     this.markers = [];     this.getnewmarker = function () {         return new marker();     }     this.addmarker = function (marker) {         this.markers.push(marker);     }  }) .controller('servicectrl', function ($scope, markerservice) {     $scope.marker = markerservice.getnewmarker();     $scope.addmarker = function () {         markerservice.addmarker($scope.marker);         $scope.marker = markerservice.getnewmarker();     }     $scope.markers = markerservice.markers; }); 

you create marker in controller , use markerservice store object.

and working demo: http://jsfiddle.net/3cvc9rrs/


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -