Angular$scope在更新时未将结果传递给父控制器
Angular $scope not passing results to parent controller on update
在我的应用程序中,我有一个$http.get()
请求,它将数据存储到数组$scope.requirements
中,并将布尔值存储到$scope.requirementsFulfilled
中。
我有一个指令,使用与页面相同的控制器。它们都在$scope.requirements
上进行ng重复。当requirementsFulfilled为false时,仅显示指令版本,当为true时,仅包含页面。
问题是,当我在第一次调用$http.get()
后,结果只存储在指令版本中。如何确保此信息与两者都绑定?
在控制器内。。。
$scope.requirementsFulfilled;
$scope.requirements = [];
$scope.getRequirements = function(url) {
$http.get(url)
.then(function(res){
$scope.requirements = res.data.requirements;
$scope.setFulfilled( res.data.fulfilled );
});
};
$scope.submitRequirementScan = function() {
if ($scope.checkRequirement() ) {
$scope.getRequirements('tempjson/requiredPartsFulfilled.json');
}
};
$scope.setFulfilled = function( inputFulfilled ) {
$scope.requirementsFulfilled = inputFulfilled;
};
$scope.getRequirements('tempjson/requiredParts.json');
页面获取需求并填充页面。然后,用户执行触发checkRequirement()
的操作,如果为true,则获取新的json。从这一点开始,只有指令在更新。
我相信正在为该指令创建一个子范围,但我不确定具体发生了什么。这是指令信息的完整性。
.directive("operationRequirements", function () {
return {
restrict: "E",
templateUrl: "requirements/requirements.html"
};
});
这是怎么回事?
edit-指令的Html
<div class="col-md-6 col-md-offset-3">
<h5>Scan Requirements</h5>
<form ng-submit="submitRequirementScan()" ng-controller="operationCtrl">
<label> <div class="glyphicon glyphicon-barcode ng-hide" ng-hide="requirement.scanned"></div>
<input type="text" ng-model="text" name="text" placeholder="Scan Barcode" autofocus /></label>
<input type="submit" id="submit" value="Submit Scan" class="btn" />
<table class="table table-hover">
<tr ng-repeat="requirement in requirements | filter : unScannedFilter">
<td>{{$index + 1 }}</td>
<td>
<div class="glyphicon glyphicon-barcode ng-hide" ng-hide="requirement.scanned"></div>
<div class="glyphicon glyphicon-check ng-show" ng-show="requirement.scanned"></div>{{requirement.scanned}}
<div class="col-md-4">
<input type="checkbox" ng-model="requirement.scanned">
</div>
</td>
<td>{{requirement.partId}} - {{requirement.partDescription}}</td>
</tr>
</table>
</form>
</div>
edit2-Html,它调用指令操作Requirements,并在页面上显示用ng-show隐藏的需求。
<div class="row" ng-hide="requirementsFulfilled" >
<operation-Requirements></operation-Requirements>
</div>
<div class="col-md-12" ng-show="requirementsFulfilled">
<table class="table table-hover">
<tr ng-repeat="requirement in requirements">
<td>{{$index + 1 }}</td>
<td>
<div class="glyphicon glyphicon-barcode ng-hide" ng-hide="requirement.scanned"></div>
<div class="glyphicon glyphicon-check ng-show" ng-show="requirement.scanned"></div>
</td>
<td>{{requirement.partId}} - {{requirement.partDescription}}</td>
</tr>
</table>
</div>
因此,这可能有助于为您指明正确的方向。我所做的是将需求的东西提取到自己的服务中。现在您有了一个singleton,它可以处理所有与零件有关的事务。当它在一个地方更新时,它在所有地方都更新了。指令不再需要其他控制器。
http://plnkr.co/edit/Nej79OI3NrKcrkMNix3D?p=preview
app.service('partsService', function() {
return {
requirementsFulfilled: false,
requirements: [],
getRequirements: function () {
this.requirements.push({partId: 1, partDescription: 'Some Desc', scanned: false});
this.requirements.push({partId: 2, partDescription: 'Some Desc 2', scanned: true});
},
submitScan: function (id) {
this.requirements.filter(function (part) {
return part.partId === id;
}).map(function (part) {
part.scanned = true;
});
this.requirementsFulfilled = this.requirements.filter(function (part) { return !part.scanned }).length === 0;
}
};
});
相关文章:
- Angular$scope在更新时未将结果传递给父控制器
- MongoDB查询结果作为循环控制器
- 如何打开模态窗体,在控制器中执行操作,并在 Grails 中呈现结果
- Json 结果 MVC 控制器通过 Jquery 到 Google 地图
- Ajax OOP 结果控制器
- 如何从视图中的控制器筛选结果
- 如何根据信号器请求的结果设置控制器范围
- jQuery解析从控制器返回的JSON结果
- 从控制器获取结果,使用t4模板生成js intellisense
- 根据控制器的结果处理视图中的内容
- 将控制器操作传递给EmberJS中的组件会返回未定义的结果
- 将SQL结果从控制器传递到chart.js
- 在测试我的AngularJS控制器时,我如何模拟$http.get承诺中的结果
- 在factroy给出结果之前,控制器正在执行
- 使用控制器调用service时获得两次结果
- 调用ASP中控制器的ActionMethod.. NET MVC从Javascript,结果在Excel工作表保存
- 尝试使用对控制器的ajax调用将搜索结果插入到表中会导致错误
- ASP.. NET MVC控制器结果返回到视图
- 将查询结果从rails控制器传递到javascript
- 在控制器中调用Angular函数,并将结果传递给指令