0

Edit: Everyone went on all kinds of tangents, but the question remains - how do I get an attribute of an html element inside an angular controller?

Here is a plnkr attempt: http://plnkr.co/edit/0VMeFAMEnc0XeQWJiLHm?p=preview

    //$scope.myElem = angular.element('testDiv'); //this breaks angular
    $scope.myElem = $document.find('testDiv'); //this doesn't break angular, but unclear what it returns
    $scope.myAttr = $scope.myElem.name; //this returns nothing

If you need convincing that this is actually necessary - I need to find when a user scrolls through a div, as is shown here: http://jsfiddle.net/rs3prkfm/ Note that this has nothing to do with the question, simply a scenario. The question is simple - how do I get an attribute's value?

References:
Useless official docs: https://docs.angularjs.org/api/ng/function/angular.element
The only useful link that still doesn't work: http://mlen.io/angular-js/get-element-by-id.html

7
  • you should write a directive and/or use ng-model for model binding. Commented Jul 16, 2015 at 18:11
  • I don't understand how a directive is relevant at all. Not being rude, just really. Ng-model would get me value, true, I need to change my question. Really looking for atributes, like element.clientHeight. Commented Jul 16, 2015 at 18:13
  • a directive will allow you to communicate cleanly with anything. Commented Jul 16, 2015 at 18:13
  • Can you elaborate on that? What benefit is there to me putting the div in a directive...? Are you suggesting its own controller/scope? Why? Commented Jul 16, 2015 at 18:15
  • 1
    let the div be a directive. Then you can require in the other controller and call functions on it. Commented Jul 16, 2015 at 18:16

3 Answers 3

1

You could use the element api https://docs.angularjs.org/api/ng/function/angular.element

There should be only very few occasions where you would really need it, though.

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

6 Comments

Danke schon, aber diese link ist in die Frage.
Also, unfortunately it doesn't work - plunk added to original post.
It works pretty well actually. There were a couple of issues with the code. First, the HTML elements had some wrong syntax. Second, the angular app was never run. And the element api does need to receive a dom object. Please see edited plunkr: plnkr.co/edit/zqHMqqiv1rfZk7F1sqbL - hope that will help you to move forward
Interesting, I solved it like this: plnkr.co/edit/0VMeFAMEnc0XeQWJiLHm?p=preview using the article here: blog.sodhanalibrary.com/2014/08/… I still think the docs are unclear as ****, but maybe that's just me. Thank you.
Sooooo....any idea on how to get the clientHeight property by any chance? Not part of the question, you already answered the q.
|
1

You should manipulate the DOM using directives. The controller should only manipulate the data of the application and offer it to the html.

If you have direct manipulation on the Controller you can't, for example, bind several views to the controller. Also, if you change the id of one tag in the html and you are manipuling it directly in you controller, the controller will break.

Read this:

http://ng-learn.org/2014/01/Dom-Manipulations/

Hope it helps.

Comments

1

Use angular.element:

var myElem = angular.element("#testDiv"); // here you get your div
var myAttr = myElem.attr("name"); // you get: "testDiv"

Remember you have limitations using jqlite. If you want full functionallity of jquery element, import a full "jquery.js" before importing your angular script.

However, to manipulate the DOM you SHOULD use directives as stated by most of the answers.

7 Comments

I am testing now, will accept answer once done. Selecting a dom attribute isn't considered manipulation though? Or is it? I just need to check it. If it matches x, I run a function that doesn't have anything to do with dom.
Ok, but why not using something like ng-click="doSomething("ofThisKind")" for that? Then in your controller your doSomething function will act according to its parameter
jsfiddle.net/rs3prkfm Need to disable something when user scrolls to bottom of div.
for that you can use ng-disabled docs.angularjs.org/api/ng/directive/ngDisabled. which can actually be a function that checks the scroll position: ng-disabled="isScrollBottom()"
Of course, but I need to detect when the user scrolls to the bottom to set ng-disabled = "isUserScrolledToBottom"
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.