Himanshu
Himanshu

Reputation: 327

Accessing ng-model data in controller using angularjs

I have a string containing Json data which is being generated by some another script(lets say script A). I have to access this data using ng-model. So what i tried is that i created an input field like below and attached ng-model to it.

`<input type="text" id="check" name="jsonName" ng-model="saveJson"></input>`

Now what i did is I stored the Json data into this input field using script A like below

document.getElementById("check").value = saveJson;

Now to access this data into angular i created a controller like

    angular.module('myapp').controller('formDataController', ['$scope',
    function($scope){
        $scope.saveForm = function(){
            console.log($scope.saveJson);
        }
    }
]);

where saveForm is a method which is called on a button click using ng-click

Now the problem is until there is any interaction in input field the $scope.saveJson gives an undefiend value. But when i write something in input field then console.log shows json data with typed value.

Please help.

Upvotes: 10

Views: 7355

Answers (1)

mindparse
mindparse

Reputation: 7225

Well you need to define $scope.saveJson as property within your controller

angular.module('myapp').controller('formDataController', ['$scope',
function($scope){
$scope.saveJson = "something";    
$scope.saveForm = function(){
        console.log($scope.saveJson);
    }
}
]);

Upvotes: 3

Related Questions