0

In a factory I construct a HTML page. This page can contain a form, so I want to get a handle on the FormController. After some Googling I've got everything working with this line of code (html is all the html in a string in a jquery selector):

html.find("input").eq(0).controller('form');

I understand that:

  • find(): it is going to find all the input elements;
  • eq(): I suppose this will select the first found item of the find list;
  • controller(): this part is unclear. I find it hard to find some documentation about this. What I do know is that you can pass ngModel or form. When passing ngModel you get the FormController of the specified control, thus not the whole form. And when specifying form you get a reference to the whole form.

So, I understand the most of, but I still don't get if controller() is an Angular function or Jquery function and how/when you can use this method.

6
  • Its part of jQLite (angular) see docs. Which if you have jQuery included, it could be considered the jQuery angular plugin Commented Mar 6, 2014 at 17:55
  • Why do you use this line, exactly? It seems to be a very bad one. Commented Mar 6, 2014 at 17:57
  • @Blackhole Well, I need a way to pass a FormController to a Factory. This factory generate some HTML and a new (child) scope. The HTML can contain a form and the HTML have some buttons. I want these buttons to be enabled based on the forms state. Commented Mar 6, 2014 at 18:02
  • Seems like you're doing DOM manipulation in a service, which is a really bad idea. Can't you use ngView for your problem, or a custom directive? Commented Mar 6, 2014 at 18:05
  • @Blackhole I know it is not ideal, but I don't know how to do it otherwise. In the factory I construct a modal popup. I can inject this factory and then use it like this: `popup = new PopUp({property1:'', property2: '', templateUrl: '/Customer/EditForm'})'. Commented Mar 6, 2014 at 18:23

2 Answers 2

1

There is no concept of "controller" in jQuery: controller() is obviously an Angular function. Here is the documentation:

controller(name) - retrieves the controller of the current element or its parent. By default retrieves controller associated with the ngController directive. If name is provided as camelCase directive name, then the controller for this directive will be retrieved (e.g. 'ngModel').

Sign up to request clarification or add additional context in comments.

Comments

1

controller() is a method added by Angular to the jQuery object. It returns the Angular controller associated with the element. See the docs including other extra methods here...

http://docs.angularjs.org/api/ng/function/angular.element

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.