There are similar questions here and here, although my use-case is a little different.
I have an object called uniqueLists
which looks like this:
$scope.uniqueLists - {
name: [
'string1',
'string2',
'string3'
// Lots of strings
],
Category: [
'string1',
'string2',
'string3'
// Lots of strings
],
designer: [
'string1',
'string2',
'string3'
// Lots of strings
]
}
I'm trying to build a search function out of this list. Currently, I can display all the list items in checkboxes on the page like this (the following code uses Jade
templating engine for Node/ExpressJS, it's easy enough to understand even if you're not familiar with it. Indent == child node of the line above it)
div(class="searchNav")
p(ng-repeat="param in searchParams") {{param[0] + ' = ' + param[1]}}
div.row-fluid(ng-repeat="(key,val) in uniqueLists")
form(ng-model="???") {{key}}
label.checkbox(ng-repeat="value in val")
input(type="checkbox", ng-model="?????")
{{value}}
The only part I'm having issues with is the ng-model
of my form and checkboxes. I want the form
's ng-model == {{key}}
. I've tried setting that but it breaks Angular. I've also tried ng-model='uniqueLists[index][0]'
but, again, Angular doesn't parse this and just makes every form's model the string uniqueLists[index][0]
.
Same deal with the input
checkboxes, I want their ng-model="{{value}}"
. Is there a way to do this in my controller perhaps? I can't think of anything that will work inside of ng-repeat
.
A small note to anyone who stumbles on this question
As you'll see in the answer/fiddle below, when you refer to object/positions in an ng-model
they aren't rendered into their correct names in the DOM, but they seem to work with Angular as though they are.
For instance, in the above code, setting ng-model="uniqueLists[key][val]"
renders in the DOM as ng-model="uniqueLists[key][val]"
, but behaves as though it's ng-model="uniqueLists[name][string1]"
.
Seems to be a bizarre quirk of Angular, this tripped me up because I was checking the ng-model
names in my browser before hooking it up to my controller, and when I saw it wasn't parsing the object for the correct values I assumed it wasn't working.
uniqueLists
!). These are checkboxes, and I want each to have anng-model
name that refers to the text in the checkbox's label. This is because it's part of a search, I have the search parameters in my controller and I want the checkboxes that they pertain to to be 'checked'. Make sense? Like if you search for "Hello", "Test1" and "Test2", on the results page the checkboxes with those strings would be checked. – Cantor