studiobrain
studiobrain

Reputation: 1215

Add JSON Object to angularjs $scope?

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!

Upvotes: 0

Views: 6625

Answers (2)

Adam Zuckerman
Adam Zuckerman

Reputation: 1641

$scope.formData.JSONObjectProperty = JSONObject;

should do the trick.

Upvotes: 2

Eugene P.
Eugene P.

Reputation: 2625

$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

Upvotes: 0

Related Questions