ga-wdi-exercises/project4

Redirect issues

Closed this issue · 12 comments

Repo: https://github.com/nrc0004/project4

I am having issues getting my page to refresh when I hit submit on the "add a new blog" form. Here is the relevant code. Also, I should note the addNew function works, I just have to refresh the page manually to get it to show up and I would like it to reload or appear as soon as I hit submit.

.controller ("BlogIndexCtrl",[
    "$state",
    "Blog",
    BlogIndexController
  ])

  .state("BlogIndex", {
    url: '/blogs',
    templateUrl: "/assets/js/ng-views/blog/index.html",
    controller: "BlogIndexCtrl",
    controllerAs: "vm"
  })
function BlogIndexController($state, Blog){
  this.blogs = Blog.query()
  this.newBlog = new Blog()
  this.create = function(){
    this.newBlog.$save().then(function(blog){
      $state.go("BlogIndex", {title: blog.title})
    })
  }
}

Blog Index.html

{{blog.title}}

{{blog.content}}

<form class="addBlog"ng-submit="vm.create()">
  <input class ="add" type="text" ng-model="vm.newBlog.title" placeholder="title" /> </br>
  <textarea type="text" ng-model="vm.newBlog.content" placeholder="Content"></textarea> </br>
  <button class ="add"  ng-click ="vm.addBlog()"> + Blog </button>
</form>

What console error are you getting?

function BlogIndexController($state, Blog){
  this.blogs = Blog.query()
  this.newBlog = new Blog()
  this.create = function(){
    this.newBlog.$save().then(function(blog){
      //add a console log (console log blog, dog) here, see if it fires
      $state.go("BlogIndex", {title: blog.title})
    })
  }
}

what response is the back end returning?

You're using Express right? My thought was that the response (most likely res, the local variable in the callback) is sending an empty response.

I am. I'm also no longer receiving any errors, its just not redirecting. It's def capturing the data, it just isnt redirecting/ refreshing the index page so I can immediately see it.

what about that console.log from earlier, is that firing?

Its not

Got it! I just took out the state.go (...)and replaced it with $state.current, {}, {reload: true} and it works now. Thanks :)

👍