如何在角度js的两个控制器中使用单个服务?

时间:2015-10-05 11:01:25

标签: javascript angularjs service ng-controller

我的索引页为

<body ng-controller="MasterCtrl">
  <div id="page-wrapper" ng-class="{'open': toggle}" ng-cloak>

    <!-- Sidebar -->
    <div id="sidebar-wrapper" ng-controller="SidebarCtrl">
         <ul class="sidebar">
            <li>
                **<a href="#/nextpage?team={{department_name}}">Page Lik data <span class="menu-icon fa fa-road"></span></a>**
            </li>
         </ul>
    </div>
  </div>
  <!-- Main Content -->
    <div ui-view></div>
</body>

我们从http://localhost:3000/employees?employee_id=764967674获得的员工数据是

[
{
location: "BLR",
department: "Core",
designation: "desig",
name: "Mr X",
gender: "M",
employee_id: 764967674,
email: "thisidas@example.com",
mobile: 7654578882,
_id: "5atsda5sdasd",
}
]

sidebar_ctrl:

/**
 * sidebar  Controller
 */

 angular.module('tool')
 .controller('SidebarCtrl', ['$scope', '$cookieStore', 'Employee', '$window', '$stateParams', '$log', SidebarCtrl]);

 function SidebarCtrl($scope, $cookieStore, Employee, $window, $stateParams, $log) {

    $scope.emp_id =  $stateParams.emp_id

    if($scope.emp_id) {
        var employee_Data = Employee.query({employee_id: $scope.emp_id}, function() {           
            $scope.department_name = employee_Data[0].department;
            alert("asd");   
        });
    }

}

员工服务是

angular.module('PdbTool')
  .factory('Employee', ['$resource', Employee]);

  function Employee($resource) {  

       return $resource('localhost:3000/employees?', { employee_id: '@id' }, {
         query: {method:'GET', isArray: true},
 });
}

还有另一个使用相同服务的ng-controller

/**
 * Profile  Controller
 */

 angular.module('PdbTool')
 .controller('ProfileCtrl', ['$scope', '$cookieStore', 'Employee', '$window', '$stateParams', '$log', ProfileCtrl]);

 function ProfileCtrl($scope, $cookieStore, Employee, $window, $stateParams, $log) {

    $scope.emp_id =  $stateParams.emp_id

    if($scope.emp_id) {
        var entries = Employee.query({employee_id: $scope.emp_id}, function() {             
            $scope.emp_name = entries[0].name;
            $scope.emp_department = entries[0].department;
            $scope.emp_location = entries[0].location;
            $scope.emp_designation = entries[0].designation;
            $scope.emp_email = entries[0].email;    
        });
    }

}

在员工模板html中

<tr>
                                            <th>Designation</th>
                                            <td>{{emp_designation}}</td>
                                        </tr>
                                        <tr>
                                            <th>Department</th>
                                            <td>{{emp_department}}</td>
                                        </tr>

值是人口,但在侧栏中

Page Lik数据

数据没有显示?用于网址重定向

0 个答案:

没有答案