0

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!

1
  • Is there something wrong with the question, warranting a down vote? Commented Feb 12, 2014 at 22:46

2 Answers 2

2
$scope.formData.JSONObjectProperty = JSONObject;

should do the trick.

Sign up to request clarification or add additional context in comments.

1 Comment

$scope.formData.data = $scope.data; was the actual trick, but Im going to accept your answer for good measure.
0
$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

2 Comments

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.
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.

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.