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!

studiobrain
  • 1,135
  • 2
  • 13
  • 35

2 Answers2

2
$scope.formData.JSONObjectProperty = JSONObject;

should do the trick.

Adam Zuckerman
  • 1,633
  • 1
  • 14
  • 20
  • 1
    $scope.formData.data = $scope.data; was the actual trick, but Im going to accept your answer for good measure. – studiobrain Feb 13 '14 at 04:33
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

Community
  • 1
  • 1
Eugene P.
  • 2,645
  • 19
  • 23
  • 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 '14 at 23:08
  • 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 '14 at 23:43