0

if it's jquery, we can do like $('table-row').clone().preprenTo($('table'));

I know it's just push a new value into an object in adding new data but I want to first insert the empty row and field first, how to add that html with angularjs?

2 Answers 2

1

John added a link to a famous question/answer regarding AngularJS. I would advice you to read that.

That said, to answer your question - in angular you do not tell it how to manipulate the dom. You tell it what data you have and how you want it presented.

I can only guess to what you are trying to do, but if you have a template (your 'table-row') and a 'destination' ('table') you would describe it in AngularJS like this:

<table ng-controller="PersonsController">
  <tr ng-repeat="person in persons">
    <td>{{person.Name}}</td>
    <td>{{person.Address}}</td>
  </tr>
</table>

That is great, but how do you add a row? Well you don't "add a row" - you add a person and AngularJS will do the adding of rows for you. You already explained how you want a person displayed.

To add a person you would have to add a person to a list/array of persons and that list would be in the scope of your view/application.

persons.push({Name: 'Bill', Address: 'Somewhere'});

You will need to attach the persons to your scope, which you will do in a controller. A controller would have to be associated with the code above with the ng-controller directive.

app.controller('PersonsController', function ($scope) {
  $scope.persons = [];
});

In the above code i assume you have a variable app pointing to your angular application. There is a small learning curve you will have to overcome, going from jquery to angular. Mostly, the way i see it, is moving your mindset from a imperative coding style to a declarative coding style.

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

Comments

1

I suspect you need a paradigm shift. Read "Thinking in AngularJS" if I have a jQuery background.

Then, if you still think that your problem is best solved by adding a row to the DOM, consider using an AngularJS directive.

5 Comments

is there any simpler way? jquery did that with one line of code, how many lines I need for angularjs?
If all you want to do is manipulate the DOM then you shouldn't use AngularJS. jQuery is fine for that purpose. If you want to build single-page applications and reusable client-side web components, then AngularJS is awesome. It's really a question of what kind of application you want to build.
I wrote in angular before but I forgot how to do it. Can u take a look?jsfiddle.net/u9gce3f2 How to add an empty field?
Sure, if you can post to Plunker or something.
I'm surprised you chose Angular 1.0.3. If that's a hard requirement then I still recommend using ngRepeat, but you may encounter other issues. If you can use a later version of AngularJS then this will do it. jsfiddle.net/fxm3eg2j

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.