Retrieving the id from Model to Controller in Angular JS

Shareer

i have a table which is having the data retrieved from an api call from my memberController which is displayed inside ng-repeat and its working fine.

I need each Business Name of the member list to link to a separate page(edit_form.html) and display the id value, so that i can pass this along with the api call to get only this particular member detail. So i have added ng-init in my edit form page which calls the function test_funct when the page loads and retrieve each persons id there. unfortunately i am unable to retrieve the id value inside the function.

HTML Template

 <div class="page" data-ng-controller="memberController">
   <table>
      <thead >
        <tr>
            <th>Business Name</th>
            <th>Contact Name</th>
            <th>Trade Balance</th>
            <th>Cash Balance</th>
            <th>Telephone</th>
            <th>Account Number </th>
        </tr>
      </thead>
      <tbody>
        <tr data-ng-repeat="member in details | filter:search">
            <td><a href="#/pages/edit_form/" target="_blank" id="{{member.id}}" class="business_name">{{member.businessname}}</a></td>
            <td>{{member.person}}</td>
            <td>{{member.balance_trade}}</td>
            <td>{{member.balance_cash}}</td>
            <td>{{member.telephone}}</td>
            <td>{{member.accountnumber}}</td>   
        </tr>
      </tbody>
   </table>
  </div>

I have the following controller

function memberController($scope, $http, $cookieStore) {
    var token = $cookieStore.get('token');
    var conId = $cookieStore.get('Cont_Id');
    var exId = $cookieStore.get('ex_Id');
    var member_list = "http://www.vb.com/functions/member_list.html?exchangeid=" + exId +
                      "&contactid=" + conId + "&token=" + token;
    $http.get(member_list)
       .success(function(response) {
           $scope.details = response;
       });

    $scope.test_funct = function(id) {
       $scope.myid = id;
       alert($scope.myid); // getting undefined in alert, i expect the id(eg:1123)
    }
 }

edit_form.html

 <div class="page" data-ng-controller="memberController">
   <div class="panel-body" ng-init="test_funct()"></div>
 </div>

Please assist me on this. Thanks in advance.

deitch

There are 2 things going on here.

First, you should separate controllers for the different views, so you end up with something like this:

<div class="page" data-ng-controller="memberController">
  <table>
    <!-- your whole big table here -->
  </table>
</div>

And your editing form as follows:

<div class="page" data-ng-controller="editController">
   <div class="panel-body"></div>
</div>

Notice that you now have two distinct controllers - your "editController" and your "memberController".

The second question then becomes, how do you transfer the selected ID from the list view ("memberController") to the edit view ("editController").

There are 2 ways of doing that.

First, you could use a service shared between the controller:

.factory('SelectedId',function() {
   return {};
});

And then in your "member" view, you would set it upon clicking:

<a href="#/pages/edit_form/" target="_blank" id="{{member.id}}" class="business_name" ng-click="setId(member.id)">{{member.businessname}}</a>

Notice the ng-click, which then needs a function in the memberController and the injected service:

.controller('memberController',function($scope,SelectedId) {
    $scope.setId = function(id) {
      SelectedId.id = id;
    };
});

While the editController retrieves it:

.controller('editController',function($scope,SelectedId) {
    $scope.id = SelectedId.id;
});

The above option works well, especially for complex things like shopping carts. If all you are doing is passing an ID, I would just stick it in the URL:

<a href="#/pages/edit_form/{{member.id}}" target="_blank" id="{{member.id}}" class="business_name">{{member.businessname}}</a>

So that the ID is part of the URL. You then can retrieve it in the "editController":

.controller('editController',function($scope,$routeParams) {
    $scope.id = $routeParams.member;
});

assuming you are using ng-route, and your route would look like:

    $routeProvider.when('/pages/edit_form/:member',{templateUrl:'/route/to/template.html',controller:'editController'});

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

mysql retrieving unique id from table with division

分類Dev

Angular js partial controller

分類Dev

How to pass json data from C# controller to angular js?

分類Dev

Retrieving an instance from the Angular injector at runtime

分類Dev

Retrieving a date from Firestore in Node.js

分類Dev

Angular: Directive watch model defined in own controller

分類Dev

Angular.js "Controller as ..." + $ scope。$ on

分類Dev

Angular JS controller throwing error

分類Dev

Retrieving the user name and email id from Facebook into my android app

分類Dev

Retrieving model using mongoose

分類Dev

How do I pass parameters (eg. id, username, password) from my ruby on rails model to my php codeigniter controller

分類Dev

How to Pass Details Model from Controller to View

分類Dev

Display Picture by retrieving from nodejs api using angular4

分類Dev

Angular View not updating after retrieving data from database

分類Dev

angular $resourse pattern /messages/:id/:controller not working

分類Dev

Setting angular.js controller variable from within a directive without ngOptions

分類Dev

Retrieving Constants\string values from a separate JS File

分類Dev

Retrieving multiple values from json data using typeahead.js

分類Dev

Pass jQuery dependency to angular js controller

分類Dev

How to get route value to controller in angular js

分類Dev

Sending id from ajax to controller for deleting data

分類Dev

set scope variable from outside controller in angular

分類Dev

ng-show from controller Angular

分類Dev

Detect a change to a paticular element of a model in angular JS

分類Dev

Laravel get model from ID with belongtoMany

分類Dev

Translate revit model to svf from bucket id

分類Dev

How to Link MVC Model data with Angular JS Model

分類Dev

How to pass data from view controller to data model in swift

分類Dev

Laravel Eloquent: Should I append values from the model or controller?

Related 関連記事

  1. 1

    mysql retrieving unique id from table with division

  2. 2

    Angular js partial controller

  3. 3

    How to pass json data from C# controller to angular js?

  4. 4

    Retrieving an instance from the Angular injector at runtime

  5. 5

    Retrieving a date from Firestore in Node.js

  6. 6

    Angular: Directive watch model defined in own controller

  7. 7

    Angular.js "Controller as ..." + $ scope。$ on

  8. 8

    Angular JS controller throwing error

  9. 9

    Retrieving the user name and email id from Facebook into my android app

  10. 10

    Retrieving model using mongoose

  11. 11

    How do I pass parameters (eg. id, username, password) from my ruby on rails model to my php codeigniter controller

  12. 12

    How to Pass Details Model from Controller to View

  13. 13

    Display Picture by retrieving from nodejs api using angular4

  14. 14

    Angular View not updating after retrieving data from database

  15. 15

    angular $resourse pattern /messages/:id/:controller not working

  16. 16

    Setting angular.js controller variable from within a directive without ngOptions

  17. 17

    Retrieving Constants\string values from a separate JS File

  18. 18

    Retrieving multiple values from json data using typeahead.js

  19. 19

    Pass jQuery dependency to angular js controller

  20. 20

    How to get route value to controller in angular js

  21. 21

    Sending id from ajax to controller for deleting data

  22. 22

    set scope variable from outside controller in angular

  23. 23

    ng-show from controller Angular

  24. 24

    Detect a change to a paticular element of a model in angular JS

  25. 25

    Laravel get model from ID with belongtoMany

  26. 26

    Translate revit model to svf from bucket id

  27. 27

    How to Link MVC Model data with Angular JS Model

  28. 28

    How to pass data from view controller to data model in swift

  29. 29

    Laravel Eloquent: Should I append values from the model or controller?

ホットタグ

アーカイブ