0

So basically I am very new to angular and node so go easy on me. I have been building a REST API for a profile site and it obviously requires updating of posts. I have GET, DELETE and CREATE working but stuck with trying to get the current post object (from stateParams) to inject into the editor.

// Uses sateParams to get object id
.controller('EditPostCtrl', ['$scope', 'Post',
      '$stateParams', '$state', function($scope, Post,
      $stateParams, $state) {
    $scope.action = 'Edit';
    $scope.isDisabled = true;
    
    $scope.post = Post.findById({ id: $stateParams.id })
    .$promise
      
  }])
<h1>Post Editor</h1>
<form name="form" ng-submit="submitForm()">
  <div class="form-group">
   <!-- If Error -->
  
   <!-- Blog Title -->
   <label>Title:</label> 
   <input type="text" class="form-control" placeholder="Example Title" autocomplete="off" required ng-model="post.title"></input>
   <br />

   <!--
   <label>Author:</label>
   <input type="text" class="form-control" autocomplete="off" placeholder="{{ author }}" required ng-model="post.author"></input>
   <br />
   -->

   <!-- Date -->
   <label>Date:</label>
   <input type="date" class="form-control" required ng-model="post.date"></input>
   <br />
  
   <!-- Post Content -->
   <label>Blog content:</label>
   <div ng-controller="editorCtrl">
   <textarea type="text" class="ck-editor" autocomplete="off" required ng-model="post.content"></textarea>
   </div>
  
   <div class="pull-right buttonspacer">
    <a href="posts" class="btn btn-default btn-lg">Cancel</a> 
    <button class="btn btn-default btn-lg">{{ action }}</button>
   </div>
  </div>
</form>

1 Answers1

0
 .controller("EditPostCtrl", function($scope,$http){
 $scope.submitForm = function(id) {
    $http.post('/api/postid/' + id)
        .success(function(data) {
            $scope.action = 'Edit';
            $scope.isDisabled = true;
            console.log(data);
        })
        .error(function(data) {
            console.log('Error: ' + data);
        });
};
});

in your angularjs,

you can use ng-click e.g.

     <button class="btn btn-default btn-lg" ng-click="submitForm(post._id)">{{ action }}</button>

can you try this

  • Firstly, thank you for your reply :) I can see you're using angular $http service. While what you have put is probably correct I am more looking for an answer using the stateParams method and using the lb-services.js that loopback generates to call on the REST API that way you don't have to use $http, think of it as an SDK -http://docs.strongloop.com/display/public/LB/AngularJS+JavaScript+SDK – Joe Hill Jul 07 '15 at 12:42