[英]Issue with AngularJS scope in controller
Below is the code of my view and an abstract of code in my controller. 下面是我的视图代码和控制器中的代码摘要。
View: 视图:
<div id="search_results" ng-controller="SearchCtrl">
<ul>
<li ng-repeat="result in results">{{result.name}}</li>
</ul>
</div>
Controller: 控制器:
myapp.controller('SearchCtrl', function($scope,SearchF) {
$scope.launch_search = function() {
SearchF.update(function() {
$scope.results = SearchF.get();
});
}
})
The function .get() returns my data, but the view does not update. 函数.get()返回我的数据,但视图不会更新。 Looks like my scope ($scope.results) does not refer to the general scope.
看起来我的范围($ scope.results)没有引用一般范围。 If I write the .update() block outside of the launch_search function, the view updates fine.
如果我在launch_search函数之外编写.update()块,则视图更新正常。
Any idea? 任何想法?
Thanks a lot everyone 非常感谢大家
.get()
is an async call, if it's using ngResource
, assign the data in the callback: .get()
是一个异步调用,如果它使用ngResource
,则在回调中分配数据:
SearchF.get({}, function(data) {
$scope.results = data;
});
解决方案是使用$scope.$parent.results
来引用父作用域。
Off course some one has to trigger your function right ? 当然有些人必须触发你的功能吗?
put a button with ng-click="launch_search()"
, on click of that your view updates. 点击您的视图更新后,按下
ng-click="launch_search()"
按钮。 In case you want the view to be updated on load check out 如果您希望在加载检查时更新视图
How to execute angular controller function on page load? 如何在页面加载时执行角度控制器功能?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.