Account Options

  1. Sign in
The old Google Groups will be going away soon.
Switch to the new Google Groups.
Google Groups Home
« Groups Home
select firing watch function twice
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
007design  
View profile  
 More options Feb 9, 5:50 pm
From: 007design <007design....@gmail.com>
Date: Thu, 9 Feb 2012 14:50:58 -0800 (PST)
Local: Thurs, Feb 9 2012 5:50 pm
Subject: select firing watch function twice
in the example below, when you change the value of the first select
field, the alert is triggered twice for some reason and the model of
the second select field is populated twice ([a,b,a,b] instead of just
[a,b])
i don't understand why it appears to be triggering the $watch function
twice.

thanks,
007

<script src="http://docs-next.angularjs.org/angular-0.10.6.min.js"></
script>

<div ng:app="MyApp">
    <div ng:controller="CtrlA">
        <select ng:options="l.name for l in list"
                ng:model="selection">
                    <option value=""></option>
        </select>
        <select ng:options="l.name for l in myValues"
                ng:model="selection2">
                    <option value=""></option>
        </select>
            Selection: {{selection.name}}
    </div>
</div>

var appModule = angular.module('MyApp', []);
appModule
    .value('MySelection',{name:''})
    .value('MyList',[{name:'test1'},{name:'test2'}])
    .value('MyOtherList',[
        {name:'test1', values:[{name:'a'},{name:'b'}]},
        {name:'test2', values:[{name:'c'},{name:'d'}]}
    ]);

function CtrlA($filter, MySelection, MyList, MyOtherList) {
    var self = this;
    self.list = MyList;
    self.otherList = MyOtherList;
    self.selection = MySelection;
    self.myValues = $filter('filter')(self.otherList,
{name:'test1'}).values;

    self.$watch('selection.name', function() {
        if (self.selection.name != '') {
            var x = $filter('filter')(self.otherList,self.selection);
            alert(angular.toJson(self.selection));
            self.myValues = x[0].values;
        }
    });

}

CtrlA.$inject = ['$filter','MySelection','MyList','MyOtherList'];

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Vojta Jina  
View profile  
 More options Feb 10, 6:04 pm
From: Vojta Jina <vojta.j...@gmail.com>
Date: Fri, 10 Feb 2012 15:04:38 -0800 (PST)
Local: Fri, Feb 10 2012 6:04 pm
Subject: Re: select firing watch function twice

I think I already refactored this example
here https://groups.google.com/forum/?fromgroups#!topic/angular/Du9x5ABru_s

Anyway, even this code does fire only once for me:
http://jsfiddle.net/vojtajina/w4Qmv/

Btw, please send jsfiddle or something like this next time, it saves us a
lot of time...

V.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
007design  
View profile  
 More options Feb 10, 8:59 pm
From: 007design <007design....@gmail.com>
Date: Fri, 10 Feb 2012 17:59:29 -0800 (PST)
Local: Fri, Feb 10 2012 8:59 pm
Subject: Re: select firing watch function twice
thanks very much for your response.  i only posted the code here as
jsfiddle was 'moving into the cloud at the time.
007

On Feb 10, 5:04 pm, Vojta Jina <vojta.j...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »