1

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!

4
  • 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" Commented Aug 10, 2016 at 15:48
  • you want to use the first or second method ? Commented Aug 10, 2016 at 16:04
  • please just say what you actually wanna do, can't understand a thing from your code . Commented Aug 10, 2016 at 16:06
  • @Sachin i want use first method :) Commented Aug 10, 2016 at 16:09

4 Answers 4

1

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>
Sign up to request clarification or add additional context in comments.

Comments

0
$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>

Comments

0

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

3 Comments

Do you want to change the object where name is 'age'? or even you don't know the property name?
i don't know property name :)
See I added a fiddle link and change code if you want to change the age on the basis of name value
0

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

3 Comments

But i wat use like ng-click=" dynamicVar = 5345345 ". If i use you method, i will need create function for sum, update, etc..
Sorry , I did't get that . Can you elaborate your question with an example or something.
sorry, my english is poor. But I can update my question, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.