How can I use a variable inside a ng-repeat expression? [closed] - angularjs

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have the following code
<tbody ng:repeat="i in data">
<tr>
<td>{{i.Dia}}</td><td>{{i.TMed}}</td>
</tr>
</tbody>
Is it possible to use a variable instead of "TMed" like "api_method"?
<td>{{i.Dia}}</td><td>{{i.api_method}}</td>
Thank you very much
Hugo

This should solve your problem:
http://plnkr.co/edit/k9hz9o5BfHGNWe9wMJCq?p=preview
If you access the key of a JavaScript object with array syntax, you can use variables:
<td>{{ i[api_method] }}</td>
You can define data and api_method anywhere, for example in MainCtrl:
app.controller('MainCtrl', function($scope) {
$scope.api_method = 'TMed';
$scope.data = [
{ "Dia": "2011-11-01", "TMed": "0.0" }
// more data...
];
});

Related

get ng-model value of span on ng-click in angular js [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I want to achieve that to get cityName whenever I clicked on city name.
Please tell me what I am doing wrong.
http://plnkr.co/edit/Vb9akn0Qx8ZhTDQxOIoy?p=preview
You can pass the value into your function, like so:
$scope.weatherUpdate = function(cityName) {
And change the HTML file, like so:
<li ng-click="weatherUpdate(x.cityName)" ng-repeat="x in cities">
This way, you can use the cityName variable within your weatherUpdate function
you should pass 'x.cityName' instead of 'cityName' in to the ng-click function.
<li ng-click="weatherUpdate(x.cityName)" ng-repeat="x in cities">
<span ng-model="cityName">{{x.cityName}}</span>
</li>
Here is you code updated.
I have passed cityName to function and assigned it to name variable you want to show.
Also I fixed part of code where you calling weather api to use selected city in query.
Hope it help.
Plnkr Working example

Angularjs on each record open new page on click [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
this is my angularjs table and response
<table class="table myCustomTable">
<thead>
<tr>
<th>Name</th>
<th>Answer</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="intends in data | filter: searchKeyword">
<td>{{intends.name}}</td>
<td>{{intends.answer}}</td>
</tr>
</tbody>
</table>
on click name i want to redirect to one page that page
I want deteils about that id
Use RouterLink directive, it let's you to link to specific parts of your application. refer this RouterLink documentation for more details.

bind parameter from a collection [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Below I have function called setProject() inside this function I have to send Id but value is not binding.
<tr ng-repeat="Project in Projects">
<td>
<div>
<input type="radio" ng-click="setProject({{Project.Id}})"/> {{Project.Nm}}
</div>
</td>
</tr>
When using ng-click you don't need to interpolate { }:
Change this:
ng-click="setProject({{Project.Id}})"
To this:
ng-click="setProject(Project.Id)"
The ng-click directive is already away of the Angular context in this case. It takes an Angular Expression as input.

convert Array to object Angularjs [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have an array like var names=["price","quality","service"], I have to convert that array to object like names=[{"name":"price", "value":"1.3"},{"name":"quality","value":"3"},{"name":"service","value":"3.4"}]. Each and every time array values are different. I need to do build the input field with that values by using ng-repeat like
<div ng-repeat="n in names">
<input name="rating" ng-model="n" value="0"/>
</div>
How I can fetch the values as objects when user submits the form. can any one help me.
Update
I need json as like this:::
names=[{"name":"price", "value":"1.3"},{"name":"quality","value":"3"},{"name":"service","value":"3.4"}]
ThanQ!
Declare you model object
$scope.item={};
Then just do
<div ng-repeat="n in names">
<input name="rating" ng-model="item[n]" value="0"/>
</div>
The data will be collected into item object.
Update: To get such in such a model, we would have to create it before binding it to view. Assuming names has all the items in the controller do:
var items=names.map(function(name){ return { name:name,value:null}; })
This creates an array of the format you require.
Now bind the view to:
<div ng-repeat="item in items">
<input name="rating" ng-model="item.value" value="0"/>
</div>

how get value select tag [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Example problem http://plnkr.co/edit/nqgujfj7msKoJ4Tm3Tlz?p=preview
The universal loade xls files
steps:
load xls file
set select value according to col data and again load
Why , on second step function changeSelect return undefined, or how on second step get value all select's user-selected!?
Short answer: You are asking for $scope.myOpt from a scope that is an ancestor of the scope that actually contains myOpt.
Long answer: This code:
<body ng-controller="MainCtrl">
...
<tr ng-repeat="row in rows">
<td ng-repeat="cell in row">
<select ng-change="changeSelect();" ng-model="myOpt" ng-options="..."></select>
Creates the following scope hierarchy:
$rootScope
MainCtrl scope <- myOpt is read here
1st ng-repeat scope
2nd ng-repeat scope <- myOpt is set here
According to JS prototypical inheritance, the ancestor scope cannot see the variable.
For your case I'd suggest passing myOpt to the changeSelect():
<select ng-change="changeSelect(myOpt)" ng-model="myOpt" ng-options="..."></select>
And:
$scope.changeSelect = function(x) {
console.log(x); // or whatever
};

Resources