Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

Well, I don't know how I can use "dynamic" variables on ng-click attribute. In this case, i want update variable from reference in ng-click ng-if etc.

My idea is update variables from reference and without create function to update this.

Controller:

$scope.elements = [
    //...
    {
        age:20,
        dynamicallyUpdateVariableWithFollowingName:"age"
    }
    //...
];

View:

<div ng-repeat="element in elements">
    <a href="" ng-click="element.dynamicallyUpdateVariableWithFollowingName = 27 ">Update AGE</a>
    <h1>You age is {{element.age}}</h1>
</div>

So, i don't want use this method:

controller:

$scope.elements = [
    //...
    {
        age:20,
        dynamicallyUpdateVariableWithFollowingName:"age"
    }
    //...
];
$scope.update = function( varname , value ){ $scope[varname] = value;}

html:

<div ng-repeat="element in elements">
    <a href="" ng-click=" update(' dynamicallyUpdateVariableWithFollowingName', 27) ">Update AGE</a>
    <h1>You age is {{element.age}}</h1>
</div>

Thanks!

share|improve this question
    
I don't recommend you to write code like "variable = value" in a ng-click, you should call a method defined in the controller. Without that, you may have problems with the refresh of the view, because only changes on the scope in which you are will be repercuted (and not the controller scope). And the answer is "element[element.aliasToAttribute] = value" – Pierre Emmanuel Lallemant Aug 10 at 15:48
    
you want to use the first or second method ? – Sachin Aug 10 at 16:04
    
please just say what you actually wanna do, can't understand a thing from your code . – Sachin Aug 10 at 16:06
    
@Sachin i want use first method :) – Olaf Erlandsen Aug 10 at 16:09

So, your question is that you want to update your data without using any function, means you don't want to use controller to update it.

So it's quite simple...

This is your .js (controller code)

$scope.elements = [
    {
        age: 20,
        dynamicallyUpdateVariableWithFollowingName: 'age'
    }
];

//Html

<div ng-repeat="element in elements track by $index">
  <a href="" ng-click="element.age = 21">Update Age</a>
  <h1>Your age is {{element.age}}</h1>
</div>

Thanks in Advance

share|improve this answer
$scope.elements = [
    {
        age:20,
        dynamicallyUpdateVariableWithFollowingName:"age"
    }
];

<div ng-repeat="element in elements">
    <a href="" ng-click="element[element.dynamicallyUpdateVariableWithFollowingName] = 27 ">Update AGE</a>
    <h1>You age is {{element.age}}</h1>
</div>
share|improve this answer

I could not understand your dynamic variable concept but see this if it match your case

$scope.elements = [  {  age:17, Name:"age1" },
                     {  age:24, Name:"age2" },
                     {  age:33, Name:"age3" }];

$scope.changeage = function(name, age){
   angular.forEach($scope.elements, function(value, key) {
      if(value.Name == name){
        value.age = age;
      return;
    }
  });
}

Your HTML:

<div ng-repeat="element in elements">
    <a href="" 
       ng-click="changeage('age' +($index +1) , ($index +1) * 20)">
         Update AGE</a>
    <h1>You age is {{element.age}}</h1>
</div>

See the fiddle

share|improve this answer
    
This is not dynamic method.. Remember, i don't know the name of final variable.. – Olaf Erlandsen Aug 10 at 16:00
    
Do you want to change the object where name is 'age'? or even you don't know the property name? – Ali Adravi Aug 10 at 16:03
    
i don't know property name :) – Olaf Erlandsen Aug 10 at 16:09
    
See I added a fiddle link and change code if you want to change the age on the basis of name value – Ali Adravi Aug 10 at 16:17

May be this will help you

HTML

   <div ng-repeat="element in elements">
        <a href="" ng-click="Update($index)">Update AGE</a>
        <h1>You age is {{element.age}}</h1>
    </div>

js part

$scope.Update=function(i){
$scope.elements[i].age="new age";
}


You can check your conditions inside the Update function

Update

Is this you are expecting , check here http://codepen.io/keephacking/pen/yJGVNx?editors=1010

share|improve this answer
    
But i wat use like ng-click=" dynamicVar = 5345345 ". If i use you method, i will need create function for sum, update, etc.. – Olaf Erlandsen Aug 10 at 15:44
    
Sorry , I did't get that . Can you elaborate your question with an example or something. – Sachin Aug 10 at 15:46
    
sorry, my english is poor. But I can update my question, thanks! – Olaf Erlandsen Aug 10 at 15:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.