2

I use routing to load different templates into a ngView. One of the templates has a simple controller which contains an array of contacts. What I'm trying to do here is as simple as by clicking a link (ngclick) call a function and add a new object into the array which I expect will be reflected in my UI.

It's something like this:

$scope.contacts = [{name='', email=''}];

<li ng-repeat="con in contacts">
  <input type="text" ng-model="con.name"/>
  <input type="email" ng-model="con.email"/> 
</li>
<li>
  <a href ng-click="addContact()">add</a>
</li>

$scope.addContact = function() {
  $scope.contacts.push( {name='', email=''} ); //-- i can use either $scope or this to reference the array and works.
}

So, the UI renders well with the initial value, the addContact function is invoked on click and I see the value is pushed (length = 2) but then the function ends the array seems to be reset to one element (lenght = 1) after angularjs evaluation.

I'm not sure if this is occurring because I use ngView. I mean, I reviewed this example (http://code.angularjs.org/1.0.3/docs/api/ng.directive:ngController) and I don't see much differences of what I'm trying to do here, the diff is that I use routing with ngView.

fiddle: http://jsfiddle.net/fdDph/

Help is much appreciated.

5
  • This should work. Can you provide a fiddle showing your problem? Commented Dec 17, 2012 at 16:15
  • Is the controller you've made $scope.contacts in being called again? That would blank it out. To just the initial value of [{name='', email=''}]; Commented Dec 17, 2012 at 18:27
  • Mark: the fiddle will be as similar as the example in the url I provided above the only diff is that I'm using a template. Not sure how to make the routing to work on jsfiddle. Mathew: I guess angularjs for some reason rebinds like I had called to load the template and blanks out the array as soon as the call leaves the function addContact(). So, my question is, do I need to tell angularjs not to do it or bind manually after ng-click? Commented Dec 17, 2012 at 18:45
  • @MathewBerg : Any comments on this? Commented Dec 18, 2012 at 13:55
  • @MarkRajcok : Any comments on this? Commented Dec 18, 2012 at 14:03

2 Answers 2

3

In your Fiddle, you are resetting the array length to 1 in the ng-show:

<span ng-hide="contacts.length = 1">

Do this and it will work:

<span ng-hide="contacts.length == 1">
Sign up to request clarification or add additional context in comments.

1 Comment

Btw, <span ng-hide="contacts"> will work too since the {{expression}} will return true when they have objects within the collection.
0

{name='', email=''} is wrong syntax btw, it should be {name:'', email:''}

2 Comments

you are right. My mistake when I wrote the code snippet. My implementation contains the correct definition of json. Any suggestion of how to solve the initial issue?
Mark found the problem as it appears ;)

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.