1

for reference, I am using a MEAN stack with Restangular for API calls.

The post.tags variable is an array of strings that my script is supposed to update (along with the rest of the object). The problem is, the tags variable is not updating while other variables within the post object have no trouble.

I join the array into a single string so that it can be easily modified in the view before updating, but I suspect that line is also what is causing it to not update correctly. I have a console log inserted to verify that $scope.tags did not update when I click the save button in the view. A little two-way binding in the view also shows that it does get updated prior to pressing save, but once that button is pressed, it reverts back to the original value.

This seems like a fairly trivial problem, but I am having trouble finding a solution. Any help would be greatly appreciated.

post-edit.js

.controller('PostEditCtrl', function ($scope, $location, $routeParams, Post) {       
  $scope.editPost = true;
  $scope.post = {};
  Post.one($routeParams.id).get().then(function(post) {
    $scope.post = post;
    $scope.tags = $scope.post.tags.join();
    $scope.savePost = function() {
      $scope.post.tags = $scope.tags.replace(/[\s]/g, '').split(',');
      console.log($scope.tags);
      $scope.post.save().then(function() {
        $location.path('/post/' + $routeParams.id);
      });
    };
  });
});

post-edit.html

...
<div class="form-group">
  <label for="content" class="control-label">Tags</label>
  <input type="text" ng-model="tags" class="form-control" id="tags" placeholder="Enter a comma separated list of tags">
  <p>{{tags}}</p>
</div>
...
4
  • Try adding the $scope.tags = $scope.post.tags.join(); line after the $scope.post.tags update in your savePost() function. You expect it to automatically get updated, but it won't, it's not bound to a member of the #scope object. Commented Apr 14, 2016 at 0:28
  • can you please try the above? Commented Apr 14, 2016 at 1:00
  • Hey sorry for the delay and thanks for the help, first time using stackoverflow and thought I set up email notifications properly... The join() was intentionally put before because it converts the array of tags into a comma separated string for the user to edit in the view. If you move it to within the savePost() function, then the view doesn't get a string that the user can edit. Commented Apr 14, 2016 at 21:48
  • The problem is after $scope.savePost() is called, any changes to $scope.tags isn't recognized and $scope.savePost() uses the original value when performing the line $scope.post.tags = $scope.tags.replace(/[\s]/g, '').split(','); Commented Apr 14, 2016 at 22:00

1 Answer 1

1

Fixed my problem by not using the temporary variable $scope.tags at all and just using $scope.post.tags. Final JS file is below:

post-edit.js

.controller('PostEditCtrl', function ($scope, $location, $routeParams, Post) {       
  $scope.editPost = true;
  $scope.post = {};
  Post.one($routeParams.id).get().then(function(post) {
    $scope.post = post;
    $scope.post.tags = $scope.post.tags.join();
    $scope.savePost = function() {
      $scope.post.tags = $scope.post.tags.replace(/[\s]/g, '').split(',');
      $scope.post.save().then(function() {
        $location.path('/post/' + $routeParams.id);
      });
    };
  });
});
Sign up to request clarification or add additional context in comments.

2 Comments

I wonder why $scope.tags is not being updated, even if it is bound to the text input.
Yeah I was wondering about that too and am honestly not too sure. For the solution I only removed the temporary variable so it must be some little quirk that I'm missing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.