我想通过从另一个js文件中发出$ scope的方法来反映$ scope的数据。但它不能。
我的来源就是这样,。
//o.delegate refer HomeCtrl's to $scope
// render Pagination by simplePagination
o.delegate.renderOn(idx)
function HomeCtrl($scope,$routeParams,...){
$scope.items = [];
$scope.renderOn(idx){
// fooo is called.
console.log("called fooo");
// changed $scope.items but $scope.items doesn't reflect view.
$scope.items.append("foo");
}
}
我想操作$ scope.items并反映我的观点。 你有什么主意吗?提前谢谢。
答案 0 :(得分:2)
您可以从角度处理循环外部调用范围更改。因此,不会自动获取更新。
您可以使用$ apply函数通知angular您更改范围,这将解决您的问题。 但是,从angular和外部脚本调用时,您应该使用不同的函数。
function HomeCtrl($scope,$routeParams,...){
$scope.items = [];
//this can be called from within angular
$scope.renderOn(idx){
// fooo is called.
console.log("called fooo");
// changed $scope.items but $scope.items doesn't reflect view.
$scope.items.append("foo");
}
//this is to be called externally
var renderOnExternal = function(idx){
$scope.$apply(function() {
$scope.items.append("foo");
});
//or even: $scope.$apply(function(){ $scope.renderOn(idx);});
}
}