Access to dynamic/programtically created $scope variables


#1

I have generated dynamic content in an <ion-content> using a javascript controller. This controller essentially creates a number of <input type="text" ng-model="pagedata.name?"> and a button that is used to process the data entered. Where ‘pagedata.name?’ varies depending on the form being generated. I build this up by basically creating the html, the $sce.trustAsHtml(html), and the $compile directive. All good at this point, the button works and the page works as if I had hard-coded it in the html template.

BUT, when the button is pressed and I want to process the content of the form I cannot dynamically access the $scope variables created. I can access them with hardcoded javascript as in $scope.pagadata.name1, $scope.pagedata.name2 etc but I need to be more dynamic such as $scope[‘pagedata.name1’] for example (I have the name of the ng-model used). Is there a way that I can dynamically get the $scope variables that I dynamically create?


#2

Solved this once I realized that $scope.pagedata is a JSON object (duh) - I was not indexing correctly. So rather than $scope[‘pagedata.name1’] it should be $scope.pagedata['name1]. If there is point notation then that needs to be expanded. Say if it is $scope.pagedata.CQ.isStudent then you can get the data by $scope.pagedata[‘CQ’][‘isStudent’].
Hope this helps others. I am still learning Ionic, AngularJS etc…