1

Suppose I have following angularjs template

<script type="text/ng-template" id="tmp">
<li>    
{{firstname}} {{lastname}}</li>
</script>

and I have two textboxes and a save button like

<input name="firstname" type="text"/>
<input name="lastname" type="text"/>
<button name="save" text="save"/>

when user enters values in firstname and lastname textboxes and press on save button I want these two values to be passed to the template and resultant html should be appended to an existing ul. How can I do it with angular?

7
  • you want to open a modal window? Commented Sep 21, 2014 at 14:24
  • no, i just want the values entered in form to be passed to template somehow and resultant html of template should append to an existing ul element that is present on the page. Commented Sep 21, 2014 at 14:26
  • you can change the state and pass the values to that state controller using services or as state params (if you using ui-router) Commented Sep 21, 2014 at 14:30
  • Is it not possible to just use an ng-repeat directive in your ul instead? It would be a lot easier. Commented Sep 21, 2014 at 14:40
  • 1
    That's fine, ng-repeat handles dynamic data just fine. Commented Sep 21, 2014 at 14:44

1 Answer 1

1

You could create a directive to dynamically compile your template and append it to the ul when someone hits the save button, but that's basically the whole purpose of ng-repeat.

Here's how it can work with an ng-repeat instead:

angular.module('main', []);

angular.module('main').controller('MainCtrl', function ($scope) {
	$scope.names = [];
  
	$scope.save = function () {
		$scope.names.push({first: $scope.firstname, last: $scope.lastname});
    }
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="main" ng-controller='MainCtrl'>

  <input name="firstname" type="text" ng-model='firstname'/>
  <input name="lastname" type="text"ng-model='lastname'/>
  <button name="save" text="save" ng-click='save()'>save</button>
  
  <ul>
  	<li ng-repeat='name in names'>{{name.first}}   {{name.last}}</li>
  </ul>
</div>

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

6 Comments

thanks a lot. great answer. Can u please tell me how I can attach edit and delete links with it.
Well, you've got an array that you're storing the results in, so you just need some UI elements that allow you to execute some $scope functions that modify items in that array. I suggest using ng-click directives to wire that up.
Ok, few things to ask. you haven't initialized app using angular.module and you have left ng-app directive to empty. can u please explain it a little bit
It's a bad practice to set up code that way but it works for quick examples. I've edited my snippet to avoid confusion.
I have created a plunk plnkr.co/edit/77yxnpmx6rQXAhzFKcyN?p=preview with delete link and relevant delete function in controller but when data gets deleted the delete link does not disappear.
|

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.