如何使用angularjs在函数中获取$scope值
How to get a $scope value inside a function with angularjs
我有一个分页代码,我应该在同一个控制器内重复使用,并认为把它放在一个函数内。但它并没有像我预期的那样起作用。总是发送一个错误,说某些值是未定义的。
我如何做到这一点。
(function () {
'use strict';
angular
.module('efutures.hr.controllers.creation', [])
.controller('UserCreateController', UserCreateController);
UserCreateController.$inject = ['$scope', '$location', '$rootScope', '$http', 'deptService', 'DeptNameService','EmployeeService'];
function UserCreateController($scope, $location, $rootScope, $http, deptService, DeptNameService, EmployeeService) {
(function initController() {
deptService.getdepts(function (res) {
$scope.depts = JSON.parse(res.data);
});
EmployeeService.GetEmpDetails(function (res) {
$scope.FilterDetails = JSON.parse(res.data); //This is the variable that I need inside the function.
$scope.PaginationTrigger($scope.FilterDetails); //CODE APPLIED HERE
});
})();
$scope.reset = function () {
$('#depts').val('').trigger('change.select2');
EmployeeService.GetEmpDetails(function (res) {
$scope.FilterDetails = JSON.parse(res.data);
});
};
$scope.paginationTrigger =function(details){ //This method is used to control the pagination
$scope.nums = ["10", "20", "30"];
$scope.viewBy = $scope.nums[0];
$scope.totalEmployees = $scope.details.length;
$scope.currentPage = 1;
$scope.itemsPerPage = $scope.viewBy;
$scope.maxSize = 5;
$scope.setPage = function (pageNo) {
$scope.currentPage = pageNo;
};
$scope.pageChanged = function () {
console.log('Page changed to: ' + $scope.currentPage);
}
$scope.setEmployeesPerPage = function (num) {
$scope.itemsPerPage = num;
$scope.currentPage = 1;
}
}
$scope.DropdownSelected = function (value) {
console.log(value.DeptName + ' Selected');
var DeptNameChanged = {
'Name': value
};
DeptNameService.DeptNameValue(DeptNameChanged, function (res) {
$scope.FilterDetails = JSON.parse(res.data);
$scope.PaginationTrigger($scope.FilterDetails); //CODE APPLIED HERE
});
};
}
})();
根据上面的代码,错误是:angular.js:13642无法读取未定义属性'length'
那么我怎么才能做到这一点呢?我很感激你的帮助。由于
test = {"a" : 1}
details = "a"
alert(test[details])
使用$scope[details].length
,因为details
是参数。
$scope.paginationTrigger =function(details){ //This method is used to control the pagination
$scope.nums = ["10", "20", "30"];
$scope.viewBy = $scope.nums[0];
$scope.totalEmployees = $scope[details].length;
$scope.currentPage = 1;
$scope.itemsPerPage = $scope.viewBy;
$scope.maxSize = 5;
$scope.setPage = function (pageNo) {
$scope.currentPage = pageNo;
};
$scope.pageChanged = function () {
console.log('Page changed to: ' + $scope.currentPage);
}
$scope.setEmployeesPerPage = function (num) {
$scope.itemsPerPage = num;
$scope.currentPage = 1;
}
}
请查看代码片段,您将得到错误。
我终于想通了。这就是我想要做到的,而且我成功了。特别感谢@Sravan一再试图帮助我。也谢谢大家的帮助。
这是代码。想分享学习
//Create a function in the controller
function paginationTrigger(value) {
$scope.nums = ["10", "20", "30"];
$scope.viewBy = $scope.nums[0];
$scope.totalEmployees = value.length;
$scope.currentPage = 1;
$scope.itemsPerPage = $scope.viewBy;
$scope.maxSize = 5;
$scope.setPage = function (pageNo) {
$scope.currentPage = pageNo;
};
$scope.pageChanged = function () {
console.log('Page changed to: ' + $scope.currentPage);
}
$scope.setEmployeesPerPage = function (num) {
$scope.itemsPerPage = num;
$scope.currentPage = 1;
}
}
/* Call the function in the desired palce.
In this case inside my service function */
EmployeeService.GetEmpDetails(function (res) {
$scope.FilterDetails = JSON.parse(res.data);
//pagination code
paginationTrigger($scope.FilterDetails); //Passing the value
});
相关文章:
- 正在获取Angular以检测$scope中的更改
- 获取Div Width,然后更新Scope Variable
- 如何使用get()获取嵌套的Scope属性
- 从Angular Scope中的Array中获取所有对象
- 在 angularjs $scope中获取数据,同时从 django 获取数据
- 如何在回调函数中获取 AngularJS 中 $scope 变量的当前状态
- AngularJS:当我已经声明$scope时,获取$scope不是定义的错误
- AngularJS:$scope.$watch 不会更新从自定义指令$resource获取的值
- 钻取$scope以获取复杂的自动完成DOM中的特定元素
- 无法从$scope获取值
- 从 JSON 获取数据并将此元素添加到变量$scope
- 获取 $scope/ngcontroller 中的元素
- 如何从控制器功能内部获取$scope
- 如何使用 angularjs 从这个$scope中获取值
- 获取$rootScope事件处理程序的$scope引用
- 从AngularJS中的服务获取数据后,如何更新$scope是最好的方法
- 在PageMod Firefox中获取Angular Scope
- 使用angular js中的另一个变量来获取$scope.x1变量
- 如何使用angularjs在函数中获取$scope值
- 在angularjs中获取$scope对象所有真值长度的最佳方法是什么