1

I want to pass the ng-repeat element to javascript function tried but it is not working

 <tr ng-repeat="x in myArry">
                <td><input type="text" value="{{x.firstname}}" id="{{x.firstname}}{{$index}}" onblur="FirstNamseCon(this.id, {{x.firstname}}{{$index}})" /></td>
                <td><input type="text" value="{{x.lastname}}" id="{{x.lastname}}{{$index}}" onblur="FirstNamseCon(this.id, {{x.lastname}}{{$index}})"></td>
                <td><input type="text" value="" id="{{x.fullname}}{{$index}}"></td>
                <!--<td><label id="{{x.fullname}}{{$index}}"></label></td>-->
            </tr>

this my javascript function

function FirstNamseCon(value1,value2) {
        document.getElementById('B Madhukar0').value = document.getElementById(value1).value + ' ' + document.getElementById(value2).value;
    }

I want to pass the name last name id to function using {{x.firstname}}{{$index}} but I am getting x is undefined error how to solve this

3
  • 1
    first of all you are using angularjs so you dont need to assign data like native javascript function. Second post your html also or make a fiddle Commented Feb 26, 2016 at 9:20
  • 1
    you have whitspaces in your ids... You aren't using angularjs... I think that you need to study how to stay in an angularjs life cycle! Commented Feb 26, 2016 at 9:20
  • no my requirement is like that i will add javascript function i need to call that function like this. that is possible or not Commented Feb 26, 2016 at 9:21

5 Answers 5

1

A angular solution will be to use ng-model to update the values

var app = angular.module('my-app', [], function() {})

app.controller('AppController', function($scope) {

  $scope.myArry = [{
    firstname: 'John',
    lastname: 'Thomas',
    fullname: 'John Thomas'
  }, {
    firstname: 'Arun',
    lastname: 'Johny',
    fullname: 'Arun Johny'
  }]

  $scope.updateFullName = function(x) {
    x.fullname = x.firstname + ' ' + x.lastname
  };
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="my-app">
  <div ng-controller="AppController">
    <table>
      <tr ng-repeat="x in myArry">
        <td>
          <input type="text" ng-model="x.firstname" ng-change="updateFullName(x)" />
        </td>
        <td>
          <input type="text" ng-model="x.lastname" ng-change="updateFullName(x)">
        </td>
        <td>
          <input type="text" ng-model="x.fullname">
        </td>
      </tr>
    </table>
  </div>
</div>

Sign up to request clarification or add additional context in comments.

1 Comment

I know this approach but i am dng modifications with value field in javascript function thats why i am using value all are working fine but passing FirstNamseCon(this.id, {{x.firstname}}{{$index}}) not working
0

use angulars ng-blur insted onblur and write your FirstNamseCon in controlles as $scope.FirstNamseCon

2 Comments

here i am calling javascript function not a angular function
is there any reason for doing it?
0

Use ng-model instead of value. Just change your code something like this:

<tr ng-repeat="x in myArry">
            <td><input type="text" ng-model="x.firstname" id="{{x.firstname}}{{$index}}" onblur="FirstNamseCon(this.id, {{x.firstname}}{{$index}})" /></td>
            <td><input type="text" ng-model="x.lastname" id="{{x.lastname}}{{$index}}" onblur="FirstNamseCon(this.id, {{x.lastname}}{{$index}})"></td>
            <td><input type="text" value="" id="{{x.fullname}}{{$index}}"></td>
            <!--<td><label id="{{x.fullname}}{{$index}}"></label></td>-->
        </tr>

But you don't have to use the onblur for this.

1 Comment

I know this approach but i am dng modifications with value field in javascript function thats why i am using value all are working fine but passing FirstNamseCon(this.id, {{x.firstname}}{{$index}}) not working
0

You can do like this -

 onblur="FirstNamseCon('firstName',$index,x)

and in your javascript code you can get id like this.

var id = x[type]+index;
 // A['firstName']+0

Comments

0

Doing DOM manipulation inside controller considered as bad pattern, don't dare to do that if you wanted to manipulate scope variable's from jQuery.

Use ng-model over the input field to get two way binding feature of angular and then you could easily pass the value of scope ng-model value to your controller.

Also make you function available inside a scope by having, $scope.FirstNamseCon = FirstNamseCon exposed FirstNamseCon method in scope by assigning its reference to scope variable.

Markup

<tr ng-repeat="x in myArry">
    <td><input type="text" ng-model="firstName" ng-clur="FirstNamseCon(firstName)" /></td>
    <td><input type="text" ng-model="x.lastName" ng-blur="FirstNamseCon(x.lastName)"></td>
    <td><input type="text" ng-model="x.fullName" ng-blur="FirstNamseCon(x.lastName)"></td>
</tr>

Comments

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.