3

I would to display JSON data in html input fields using AngularJS. Here's the code.

I have two HTML files. First HTML file contains a table which displays the json data. When you click in a row then it opens a modal window with a form (second HTML file).

Here's the Code: JSON data

var examples = [
   { 'id': '1', 'fname': 'john' },
   { 'id': '2', 'fname': 'bonnie' },
   { 'id': '3', 'fname': 'joey' }
];

Here's the Code: (First HTML file)

...
<tr ng-repeat="item in examples" ng-click="open(this.item)">
   <td>{{ item.id }}</td>
   <td>{{ item.fname }}</td>
</tr>
...

Second HTML file:

...
    <input type="text" class="form-control" placeholder="ID" id="firstVal" value="">
...

My Controller:

...
$scope.open = function (item) {
   console.log(item.id);
   $("#firstVal").val(item.id);
};
...

It doesn't insert the value "id" in the input field. How can I do this with AngularJS or jQuery?? In the console I'm getting the value of id.

3
  • 1
    how do I think in angular if i have jquery background... get away with ids.. thats not how you work in angular... using $(#...) in angular is never never needed... Commented May 5, 2015 at 9:14
  • add ng-model to the <input ... /> and set its value Commented May 5, 2015 at 9:14
  • Try: placeholder="{{item.id}}", no need for JQuery as pointed out above Commented May 5, 2015 at 9:22

2 Answers 2

6
<input type="text" class="form-control" placeholder="ID" ng-model="selectedId">

and

$scope.open = function (item) {
   console.log(item.id);
   $scope.selectedId = item.Id
};
Sign up to request clarification or add additional context in comments.

4 Comments

Hej entre.. I've tried your solution but it doesn't work. In the modal window the inputfield is remaining empty.
Do you have different controllers?? Is your console.log working??
I've found the problem. in my controller I've forgotten in my modalInstance variable the point resolve: { selected: function() { return $scope.selectedId }}
thats where I was coming to... if you have different controllers, then you would need to pass the data between them..
2

Also, make sure your second HTML is using the same controller (shared scope). Moreover, it is always advised to use a dot notation when accessing scope items. In this case you would suggest the same as entre but with a minor modification:

$scope.open = function (item) {
   $scope.selectedObj.id = item.Id
};

And in the form HTML (once you have the same controller working there):

<input type="text" class="form-control" placeholder="ID" ng-model="selectedObj.id">

UPDATE Here's a working fiddle for you: https://jsfiddle.net/aqs9vLd8/1/ Note how $scope.selectedObj needs to be initialized first as an object (empty).

Let me know if it works for you

1 Comment

Thx Rhaziel for your post. I've found my solution. You can read it in my last comment above. :)

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.