我是 angularjs 的新手,一直在尝试使用 angularui 模块来构建手风琴。对于每个手风琴标题,我都有一个嵌套标签,可以调用我的服务工厂。服务工厂根据 id 返回数据并更新我的内部手风琴内容,但是它会为所有手风琴标题更新它。因此,换句话说,单击手风琴标题将为所有手风琴 div 加载相同的内容。我希望它只返回到单击的标题。我相信我需要更多帮助来了解我的工厂服务范围。所以我的问题是是否有人可以帮助我了解如何让我的服务工厂只更新它的调用者。
我的html:
<accordion close-others="false">
<accordion-group ng-repeat="dept in departments">
<accordion-heading>
<span ng-controller="DeptHeaderCtrl" ng-click="loadEmps(dept.DeptID)">
{{dept.DepartmentName}} ({{dept.DepartmentShortName}})
</span>
</accordion-heading>
<div ng-controller="departmentList">
<div ng-repeat="emp in deptemps">
{{emp.Name_First}}
</div>
</div>
</accordion-group>
angularjs工厂服务代码:
app.factory('DeptFactory', function ($http, $rootScope) {
var sharedDeptEmpList = {};
sharedDeptEmpList.EmpList = '';
sharedDeptEmpList.fetchDeptEmps = function (deptid) {
var dept = { "userid": userid, "deptid": deptid };
$http({ method: 'POST', url: 'api/Fetch/DeptEmployees/', data: dept }).
success(function (data, status, headers, config) {
EmpList = data;
sharedDeptEmpList.broadCastEmpList();
}).
error(function (data, status, headers, config) {
alert('error');
});
};
sharedDeptEmpList.broadCastEmpList = function () {
alert('broadcasting');
$rootScope.$broadcast('handleBroadcast');
};
return sharedDeptEmpList;
});
接收广播的 Angularjs 控制器:
app.controller('departmentList', function ($scope, $http, DeptFactory) {
$scope.init = function (p_userid) {
userid = p_userid;
};
$scope.$on('handleBroadcast', function () {
alert('broadcast received');
$scope.deptemps = EmpList;
});
});