Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm struggling with something that might be very easy, but hours of searching on Stackoverlow haven't helped.

I'm using Sir Trevor with MEANJS. Sir Trevor applies itself against a textarea field and saves its content as a JSON String.

Sir Trevor is saving its content to a field Content, which is set as an Object type in a Mongoose schema.

Creating works great and everything saves as expected.

However, when editing, the data doesn't come up correctly. The textfield is assigned data-ng-model="article.content" [the 'content' field from the model], but displays as [object Object], so when Sir Trevor tries to parse the value, it errors out.

I've tried using a directive with $formatters to change the value:

    <textarea data-ng-model="article.content" id="content" 
class="form-control st-instance" placeholder="Content" stRaw>
</textarea>

...and here is the directive:

articleApp.directive('stRaw', function(){
return {
    restrict: 'A',
    require: 'ngModel',
    link: function(scope, element, attr, ngModel) {

        function stringIt(val) {
            return JSON.stringify(val);
        }

        ngModel.$formatters.push(stringIt);

    }
};

});

But it doesn't seem like the directive is ever triggered [I tried console.log within the link function and never saw anything].

I was able to make it work fine before by changing the schema type String and then using stringify over and over again within the code. This seemed sloppy, created undue bloat and also created challenges when trying to iterate through on an actual view page [it was seen as a String -- couldn't figure out how to parse].

I'm assuming I need to somehow catch the article.content attribute before it's rendered and change the value to a string. Is that the right direction?

share|improve this question
    
can you give us a console.log(article.content) –  ma08 Aug 2 '14 at 7:26
    
@ma08 - doing a console.log(article.content) on the server-side code [finding the article based on id] returns "{ data: [ { data: [Object], type: 'heading' }, { data: [Object], type: 'text' } ] }" ... but, running the same console.log from the angular side [in the code used to populate the edit screen], it comes back undefined. –  user531023 Aug 2 '14 at 7:31
    
and on the client side? –  ma08 Aug 2 '14 at 7:32
    
Hi @ma08 - I hit enter too soon, so had to edit my reply. =) Running console.log(article.content) within the code where I set $scope.article to the found article from the server-side query results in undefined. Inspecting $scope in firebug shows content is populated with an object with the expect key:value pairs. –  user531023 Aug 2 '14 at 7:36
    
If it makes any difference, the rest of the fields display correctly -- title, slug, tags, etc. It's really just this block I'm having challenges with. –  user531023 Aug 2 '14 at 7:37

1 Answer 1

up vote 3 down vote accepted

There is typo in your html, the stRaw should be st-raw.

<textarea data-ng-model="article.content" id="content" class="form-control st-instance" placeholder="Content" st-raw></textarea>
share|improve this answer
    
Sheesh -- I hate it when I overlook such a simple thing. Thanks! –  user531023 Aug 2 '14 at 15:07
    
Your welcome :) I frustrated with this kind of problems very so often myself, when I start to learn angularjs. –  runTarm Aug 2 '14 at 15:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.