如何从其他方法访问Angular.js$scope
How to access Angular.js $scope from other method?
例如,我的服务器收到了这样的请求。作为响应,我将获得一个带有参数的对象数组:id、name和position。所有这些都加载到一个表中。如果我稍后决定更改数组$scope.employees
,我该如何操作它?
来自服务器的答案是:
data = [{"id":1,"name":"Jack","position":"City guard"},{"id":2,"name":"Jim","position":"Sheriff"},{"id":4,"name":"Jack","position":"Cruel genius"},{"id":7,"name":"Guy","position":"Manager"}]
如何确保该请求已经发布到表中,以便我可以执行一些稍后的操作?
angular
.module('MyApp', [])
.controller('MyController', ['$scope', '$http', MyController]);
function MyController ($scope, $http) {
$http.get("/servlet").success(function(data){
$scope.employees = data;
});
}
function otherOperation () {
$scope.employees.push({
id : 5,
name : "John",
position : "Manager"
});
}
HTML代码:
<div id="content" ng-controller='MyController'>
<table id="table">
<tr>
<th> ID </th>
<th> Name </th>
<th> Position </th>
</tr>
<tr ng-repeat="employee in employees">
<td>{{employee.id}}</td>
<td>{{employee.name}}</td>
<td>{{employee.position}}</td>
</tr>
</table>
<button ng-click="otherOperation"> Button </button>
</div>
otherOperation方法应该嵌套在MyController中,如下所示:
angular
.module('MyApp', [])
.controller('MyController', ['$scope', '$http', MyController]);
function MyController ($scope, $http) {
function otherOperation () {
$scope.employees.push({
id : 5,
name : "John",
position : "Manager"
});
}
$http.get("/servlet").success(function(data){
$scope.employees = data;
});
}
您也可以将$scope作为参数传递,如下所示:
angular
.module('MyApp', [])
.controller('MyController', ['$scope', '$http', MyController]);
function MyController ($scope, $http) {
$http.get("/servlet").success(function(data){
$scope.employees = data;
});
otherOperation($scope);
}
function otherOperation ($scope) {
$scope.employees.push({
id : 5,
name : "John",
position : "Manager"
});
}
相关文章:
- Angular JS IE9 Hashbang url rewriting
- 无法在数据endVal中设置值=“”;{{ucount}}”;使用Angular JS的CountUp
- angular.js没有'无法在PhoneGap中处理视图标记
- Angular js-返回一个包含类似
- 如何使用 Angular JS 将数据保存在数据库中
- 将Angular js与taglib结合使用
- Angular JS Filter-通过3个复选框进行筛选
- vaadin:使用自定义布局集成angular js
- angular js密码强度显示问题
- 使用angular.js问题的JavaSpringREST服务
- 将复杂对象从angular js传递到web api,它总是返回404
- entre上的angular js阻止提交
- 根据Angular.JS上一次的内容禁用选择
- 如何在angular js中使用指令.不适用于我的情况,为什么
- 预期响应包含一个对象,但在angular js中得到一个数组错误
- angular.js:13424错误:[ng:areq]参数'fn'不是函数,getObject
- 使用Angular js-量角器e2e测试上传文件
- Angular.js延迟控制器初始化
- 如何在angular.js中动态应用自定义过滤器
- 谷歌地图/Angular JS:地图更新并不总是反映数据,并且可以't删除标记