admin管理员组文章数量:1389754
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.
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.
Share Improve this question edited Feb 3, 2015 at 12:48 ashishkumar148 1,0031 gold badge11 silver badges27 bronze badges asked Feb 3, 2015 at 11:59 HimanshuHimanshu 3273 silver badges9 bronze badges 7-
you should always set scope variable like
$scope.saveJson = saveJson
from your controller.setting scope variable outside angular will not run the digest cycle , scope variable will not update..you need to do$scope.$apply()
to get updated value of all scope variables – Pankaj Parkar Commented Feb 3, 2015 at 12:02 - how should i use this $scope.$apply() – Himanshu Commented Feb 3, 2015 at 12:33
-
you could use
if(!$scope.$$phase) $scope.$apply()
but this is bad code in terms of angular.' – Pankaj Parkar Commented Feb 3, 2015 at 12:35 - I used but its not working – Himanshu Commented Feb 3, 2015 at 12:48
- you should do it before accessing $scope, inside controller – Pankaj Parkar Commented Feb 3, 2015 at 12:52
1 Answer
Reset to default 3Well 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);
}
}
]);
本文标签: javascriptAccessing ngmodel data in controller using angularjsStack Overflow
版权声明:本文标题:javascript - Accessing ng-model data in controller using angularjs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744698107a2620403.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论