Refer to this example.
I got an array named Header (store name for table header) and another array named Type (store class name for each column). I wonder how to get access to the Type array using Header index.
It appears that in a binding expression like that you need to call $index with brackets, i.e.:
$root.Type()[$index()]
Updated fiddle: http://jsfiddle.net/KuzGf/1/
Related
I am trying to bind my selected category's guid property to another ko.observable element. I need to hold that data value in order to send it to the server in a correct JSON format.
Jsfiddle
I am stucked at the binding selected category's guid value to SelectedCategoryGuid in order to appear in JSON file like
'SelectedCategoryGuid': 'guid1'
I have tried $data and $root bindings in the HTML but couldn't achieve it.
Step 1: Remove the quotes around your value data-bind. You should pass a reference to an observable here, not the name of a property.
value: Info.SelectedCategoryGuid
Now, you'll see your guid paragraph print: [object Object]. That's because it's storing the whole category, not just the Guid.
Step 2: To only store the Guid property, use the optionsValue binding. This binding works similar to the optionsText binding you've already used:
optionsValue: 'Guid'
Now things start working as intended. You'll notice the initial bla value gets cleared, because it does not appear in your data set.
Here's the two changes in a fiddle: https://jsfiddle.net/40sh1vjj/
I am searching a solution where I can replace a object content with other in a array of objects.
The thing is, I don't know what my function will pass, so I can't pass the values inside as keys directly, so the reference won't be copied, is there any way I can assign the value directly without passing the reference? I know that objects pass references and not values, but is there a way to do that?
I tried two ways:
state.document["atributes"].splice(state.document["atributes"][state.currentIndex],1,section);
And
state.document["atributes"][state.currentIndex] = section
Where my state.document["atributs"] is my array, and the state.currentIndex the index where I want to replace the element inside my array.
What happens at the moment is that my object can be a Table a paragraph etc.
If the objects are the same it replaces the content :/
Any help with this? Thank you
If you're only modify what in the currentIndex, try
Vue.set(state.document["atributes"], state.currentIndex, section)
Reference: Vue documentation
I am hitting an API which retrieves a nested JSON and setting it to my $scope.data variable.
I do an ng-repeat like ng-repeat="event in data".
and try to access a value in the JSON {{event.src.#userID.title}}
There is an error
Lexer Error: Unexpected next character at columns 14-14 [#] in expression [event.src.#userID.title].
When I forcefully remove the # from the JSON returned from the API and access as {{event.src.userID.title}} it works properly.
Please help so that I can access value with the # in the key name.
The API that I hit returns a list [{"":""},{},{},{}]
{"":""} is a nested list
You have to use a different syntax to access an object property whose name isn't a valid variable name:
{{event.src["#userID"].title}}
What should be the right behaviuor when you have a ng-model declaration like this one?
data-ng-model="elements[0]"
The way it works, if elements is already defined in the scope as an array, it works as I'd expected assigning the first element of the array.
But if elements is not declared it assigns this value :
elements = {0:'anyvalue'}
(which makes sense if I'd had written data-ng-model="elements['0']")
In this case :
elements[0]='anyvalue';
elements['0']='anyvalue';
and I cannot read the value of the propery using "dot" notation (elements.0 or elements.'0').
So it looks correct, but a bit weird.
Is this behaviour correct, or it should instantiate an array when the scope variable is not defined?
An array is just a special type of object. If you look at an array in a debugger, all of the values are listed as properties with numeric keys, like the one you show. If you don't initialize the object as an array, it would still accesses the object in the same way, which just means you now have an object with numeric keys and none of the helpful functions from the Array prototype.
I have a variable param in scope as $scope.param that is always either foo or bar. I'm creating a table with rows defined by <tr ng-repeat="d in data">.
When I have the following: <td>{{d.foo}}</td> or <td>{{d.bar}}</td> everything works and the data shows up fine. However, when I have <td>{{d.param}}</td> angular can't find anything and the cell is blank.
In other words, I'm trying to access an object value using a variable as the key rather than the key itself. Any idea how to do this?
Use bracket notation:
{{d[param]}}