i need to set the model for a textfield to one of a number of items
but can't seem to figure out how to do it in 10.6
in 10.5 i could do something like:
ng:model="getMyModel('filterString')"
and in the controller do:
getMyValues = function(filterString) {
for (var a in self.list) {
if (self.list[a].name === filterString)
return self.list[a].values;
}
};
since jsfiddle is down i must post the code here but here is an
example:
<div ng:app="MyApp">
<div ng:controller="CtrlA">
<select ng:options="l.name for l in list"
ng:model="selection">
</select>
<textarea type="myWidget"
ng:model="getMyValues()"></textarea>
Selection: {{selection.name}}
</div>
</div>
function CtrlA(MySelection, MyList) {
var self = this;
self.list = MyList;
self.selection = MySelection;
self.getMyValues = function() {
for (var a in self.list) {
if (self.list[a].name === selection.name)
return self.list[a].values;
}
};
}
CtrlA.$inject = ['MySelection','MyList'];
angular.inputType('myWidget', function() {
this.$parseModel = function() {
if (this.$modelValue) {
var x = "";
for (var a in this.$modelValue)
x += this.$modelValue[a].optionValue+' and ';
this.$viewValue = x;
}
};
this.$parseView = function() {
var vals = this.$viewValue.split(' and ');
for (var a in vals)
this.$modelValue[a].optionValue = vals[a];
};
});
angular is awesome it's just taking some work to get my hear around
it.
thanks very much!
007
say you have a list of products which each have several options
(price, size, color, etc). some of these options are global to all
items (price) but others are specific to each product. for example,
if you have a "shirt" product, it would have a "size" option but a
"blanket" product might only come in one size so it needs no "size"
option.
the list of global options and their possible values is maintained
separately from the individual product options and their values.
if you examine the fiddle above you can see where i am at the moment.
it works but it's quite ugly. can anyone give me some idea of how to
clean this up, a better way to do it?
Wow, this is amazing. I was starring at that code for few minutes and still I have completely no idea what does it do and what does it do it for. The part with loops in loops in loops in etc... is plain crazy, hehe :) I would definitely reject that code and sit down with developer to try to figure out the true idea behind that.
Regards, Witold Szczerba
On 13 February 2012 02:02, 007design <007design....@gmail.com> wrote:
> say you have a list of products which each have several options > (price, size, color, etc). some of these options are global to all > items (price) but others are specific to each product. for example, > if you have a "shirt" product, it would have a "size" option but a > "blanket" product might only come in one size so it needs no "size" > option. > the list of global options and their possible values is maintained > separately from the individual product options and their values.
> if you examine the fiddle above you can see where i am at the moment. > it works but it's quite ugly. can anyone give me some idea of how to > clean this up, a better way to do it?
> thanks a lot for all your help! > 007
> -- > You received this message because you are subscribed to the Google Groups "AngularJS" group. > To post to this group, send email to angular@googlegroups.com. > To unsubscribe from this group, send email to angular+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/angular?hl=en.
see if this makes it clearer.
each product contains a list of options (size, color, price, etc.)
each of these product options has a value (small, red, $42, etc)
"static options" are options which EVERY product has. for example,
every product has a price but not all products have a size or a color.
when the app loads, it grabs the list of "static options" from the
server and the list of products. they're two separate models.
it then displays the list of products. selecting a product will
display the options for that product in select fields with the
appropriate values selected (or just as text fields if there is not
more than one possible choice).
here's the tricky bit. the ng:model of the select field is the
'value' property of each product (i.e. product.name="shirt,
product.value = "small").
if the product option is a 'static option', ng:options for the select
field should be the values of the static option object which matches
the product option.
so, what all those ugly loops do is look thru each product option
matching it's name property against the name of each static option
object. when it finds the matching option it adds a property called
staticValues to the product object consisting of the values for the
matching static option. also, it sets the 'value' property of the
product to the matching static value object instead of just a string.
it's quite awful, i know, but the only way I can think of to get
around it. how do you define relationships between two objects
retrieved at different times?