0

I have a page where html elements will be generated dynamically from server. I want to add ng-model directive to a textbox based on some codition using jquery. for example:

$(document).ready(function(){
if(current page is 'something') //dont bother about the condition!
  $('input[title="test"]').attr('ng-model','name');
});

so that i can use ng-bind to bind the value of this text-box in the page. How do i achieve this.?

Vikas Kottari
  • 495
  • 2
  • 10
  • 24

1 Answers1

4

This is a bad idea. Your code is running in document.ready, which angular knows nothing about, and you then add elements to the DOM and don't tell angular about them, as such, it's not going to work.

You need to think about things differently - angular has a router specifically for showing different views (or elements) on different pages, so you shouldn't be using things like:

if(current page is 'something')

Furthermore, if you read just a small amount on angular, you'll be constantly reminded that directives manipulate the DOM, and that you should avoid doing it elsewhere in almost all other cases.

I strongly recommend that before you go any further, you read more about angular to get a firm understanding of how it works, particularly in relation to jQuery (you really, really don't need jQuery if you have angular).

It sucks, but you're going to have to learn more before you go any further, because otherwise you'll just end up getting frustrated and creating an app that is just 'wrong'...

Community
  • 1
  • 1
Ed_
  • 18,798
  • 8
  • 45
  • 71