从控制器外部的 JS 触发作用域函数
Trigger scope functions from JS outside the controller
我在 SO 上看到了对此的其他回应,但要么我不明白这些回应在我的情况下是如何工作的,要么我想多了;示波器一直是我的弱项。
我目前正在使用包含 iframe 的模态 windows 的颜色框。为了在 angular 中使用它,我创建了一个属性指令,它采用锚标记的 URL 并使用它来触发颜色框。通常,当用户在模式中执行我需要的任何操作时,我已经让 JS 在父 window 中触发某些内容以避免重新加载。这在 Angular 中似乎更为重要,我正在尝试弄清楚如何让 colorbox modal 与 Angular 通信。到目前为止,我能想到的最好的办法是让模态的 JS 更改输入(或多个输入)的值来定义我想要它做什么。
有没有easier/more直接的方式?
编辑:感谢下面的解决方案,我找到了答案:
var appElement = parent.document.querySelector('[ng-app=gamersplane]');
var $scope = parent.angular.element(appElement).scope();
$scope.$apply(function() {
// Do your angular stuff in here!
});
键是元素选择器上的父元素 AND 在 angular.element 之前。
据我所知,这是一个 angular 问题。
我认为这个问题会有所帮助
How to change AngularJS data outside the scope?
正如您在 fiddle 中看到的 http://jsfiddle.net/jeremy/kj8Rc/
更改函数是全局函数,我的意思是不在 angular 执行范围内。
function change() {
// here it fetches the mandatory, and unique (?tobeconfirmed) ng-app HTML element.
var appElement = document.querySelector('[ng-app=myApp]');
// it finds the scope for you, given that previous element, i believe it s root scope in that case as ng-app.
var $scope = angular.element(appElement).scope();
// it s kind of scope binding, it will execute this function within angular scope of execution
$scope.$apply(function() {
// then finally appply his change within angular scope of execution, doing so effectively apply the changes
$scope.data.age = 20;
});
}
我在 SO 上看到了对此的其他回应,但要么我不明白这些回应在我的情况下是如何工作的,要么我想多了;示波器一直是我的弱项。
我目前正在使用包含 iframe 的模态 windows 的颜色框。为了在 angular 中使用它,我创建了一个属性指令,它采用锚标记的 URL 并使用它来触发颜色框。通常,当用户在模式中执行我需要的任何操作时,我已经让 JS 在父 window 中触发某些内容以避免重新加载。这在 Angular 中似乎更为重要,我正在尝试弄清楚如何让 colorbox modal 与 Angular 通信。到目前为止,我能想到的最好的办法是让模态的 JS 更改输入(或多个输入)的值来定义我想要它做什么。
有没有easier/more直接的方式?
编辑:感谢下面的解决方案,我找到了答案:
var appElement = parent.document.querySelector('[ng-app=gamersplane]');
var $scope = parent.angular.element(appElement).scope();
$scope.$apply(function() {
// Do your angular stuff in here!
});
键是元素选择器上的父元素 AND 在 angular.element 之前。
据我所知,这是一个 angular 问题。
我认为这个问题会有所帮助 How to change AngularJS data outside the scope?
正如您在 fiddle 中看到的 http://jsfiddle.net/jeremy/kj8Rc/
更改函数是全局函数,我的意思是不在 angular 执行范围内。
function change() {
// here it fetches the mandatory, and unique (?tobeconfirmed) ng-app HTML element.
var appElement = document.querySelector('[ng-app=myApp]');
// it finds the scope for you, given that previous element, i believe it s root scope in that case as ng-app.
var $scope = angular.element(appElement).scope();
// it s kind of scope binding, it will execute this function within angular scope of execution
$scope.$apply(function() {
// then finally appply his change within angular scope of execution, doing so effectively apply the changes
$scope.data.age = 20;
});
}