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.

How do I inject my JSON Object in my angular $scope upon create()?

html:

<input type="text" class="title" placeholder="hold" ng-model="formData.text"/>
<input type="text" class="desc" placeholder="description" ng-model="formData.desc"/>

<button type="submit" class="btnCreate" ng-click="createRule();direction('front');go('/myrules')">CREATE
</button>

controller:

$http.get('/public/mdm/default.json').success(function (data) {

            $scope.data = data;
            console.log($scope.data);
        })

$scope.formData = {};

$scope.createRule = function () {
            Rules.create($scope.formData)
                .success(function (data) {
                    $scope.formData = {};
                    $scope.rules = data;

                    // JSON please join my creation...
                });
        };

$scope.formData is the form poulation. It is an Object so push() is out...

$scope.formData[JSONObject] = $scope.data; does not get added properly.

I feel this is a much simpler process than it currently appears to me. Any direction is appreciated so Thanks in advance!

share|improve this question
    
Is there something wrong with the question, warranting a down vote? –  studiobrain Feb 12 at 22:46
add comment

2 Answers

up vote 1 down vote accepted
$scope.formData.JSONObjectProperty = JSONObject;

should do the trick.

share|improve this answer
1  
$scope.formData.data = $scope.data; was the actual trick, but Im going to accept your answer for good measure. –  studiobrain Feb 13 at 4:33
add comment
$scope.formData[JSONObject] = $scope.data;

JSONObject - I don't believe it's valid key for formData object hash.

have you tried to change it to vital name ?

$scope.formData["JSONObject"] = $scope.data

or

just do simple merge of objects, i.e. $scope.data -> $scope.formData

related question, I believe AngularJS: factory $http.get JSON file

share|improve this answer
    
SyntaxError: Unexpected token o It is already a parsed as a js Object and I can get all data from within, the trouble is adding it to my scope. –  studiobrain Feb 12 at 23:08
    
finally, I got idea, take a look, updated. –  Eugene P Feb 12 at 23:20
    
I had an idea that maybe I could just wrap both $scope.data and $ scope.form Data in an array. I have a factory that the rule is created within. Ill update when back in front of my computer. –  studiobrain Feb 12 at 23:43
add comment

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.