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?