Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm sticking my toe in the Angular ocean. I have a web application that uses jQuery to allow the user to interact with the application using input forms and buttons. I'm learning Angular and as an exercise, I decided to try to write code to do this with Angular instead.

<body ng-app="myApp" ng-controller="MainFormCtrl as Form">

I used the ng-app and ng-controller directives as above to make my application use an Angular controller I've written.

I use the ng-model directive with each input the user can enter data into. I use the form ng-model="Form.inputs.xxxxx" because I want to be able to pass around the collection of input values as a single object.

I have written code in my controller that provides default values for these inputs. I am still using my existing code to read the values from the DOM. I understand that once I've declared my model and controller, Angular magically allows me to pass around the model so that I can read it and make changes to it elsewhere in my program.

I've attached a method called getInputs to my Angular controller. How can I call this member function (or get other members of the controller object) from non-Angular code? If that isn't practical, what sort of solution should I use.

share|improve this question
    
what does the non-angular code do, exactly? Angular is just a framework, it isn't changing the way that JavaScript operates; That being said, there are things you can do to ensure that Angular is able to keep up with what you are doing. –  Claies May 26 at 0:29
    
the short answer is probably this: if your ng-model is Form.inputs.xxxxx, Angular will automatically create a variable in it's $scope object to track changes to that model. Your controller would have access to $scope.Form.inputs.xxxxx as a value, two way bound. –  Claies May 26 at 0:32
    
I have a few minutes, we can use a chat and try to discuss your specific case, if you would like? let me know. –  Claies May 26 at 0:36
    
actually, in your code, since you are using the ControllerAs syntax, inputs would just be a property of your controller, so inputs.xxxxx would work, without using $scope. –  Claies May 26 at 0:43
    
I'm not sure if my question was clear to you, @Claies. I am trying to access the contents of the controller from non-Angular code. I'm not sure if that's allowed. I am in the AngularJS chatroom now. –  Daniel Allen Langdon May 26 at 1:31

1 Answer 1

up vote 1 down vote accepted

You can access controller from non-angular code by

$("[ng-controller='MainFormCtrl']").scope()

Edited to show how to access scope & all method within controller using Jquery(above) & non-Jquery(below)

document.getElementById('yourControllerElementID').scope()
share|improve this answer
    
This isn't working. I just get an array with one element in it, and I don't see any way to access the controller from it. –  Daniel Allen Langdon May 26 at 1:37
    
Also, I'm trying to use Angular instead of jQuery, but this answer uses jQuery. Doing a quick search, it seems like there should be some way to do this without using jQuery. –  Daniel Allen Langdon May 26 at 1:41
    
Thanks for helping out! –  Daniel Allen Langdon 2 days ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.