Javascript 访问角度对象';从JS外部调用函数

Javascript 访问角度对象';从JS外部调用函数,javascript,scope,angularjs,Javascript,Scope,Angularjs,我正在用AngularJS框架构建一个HTML应用程序。我有一些遗留JavaScript操作需要访问Angular对象中的函数,但我无法让它工作。 这是角度对象(我需要访问的函数是$scope.info()): 我试图通过angular.element('content').scope().info('me')访问它,但没有结果(控制台显示undefined)。我试图转储angular.element('content').scope()的结果,得到了完整的对象列表和所有内容。理论上,我的第一个

我正在用AngularJS框架构建一个HTML应用程序。我有一些遗留JavaScript操作需要访问Angular对象中的函数,但我无法让它工作。
这是角度对象(我需要访问的函数是
$scope.info()
):

我试图通过
angular.element('content').scope().info('me')
访问它,但没有结果(控制台显示
undefined
)。我试图转储angular.element('content').scope()的结果,得到了完整的对象列表和所有内容。理论上,我的第一个例子应该是可行的,但是它不可行

非常感谢任何关于我如何实现这一目标的指导
(PS:未解决此问题)


当我最终能够访问该函数时,它没有按预期工作–技术上修改了
$scope.text
的值,但HTML中使用的表达式没有更新!例如,从外部函数调用此函数后,
{{text}}

不会更新

有没有办法解决这个问题?

angular.element()
需要一个DOM元素,因此如果您有这个html:

<div ng-controller="content">
</div>
或者不使用jQuery:

angular.element(document.getElementById('myDiv')).scope().info('me')
应该有用

编辑:

如果您更改了范围中的某些内容,则可能需要使用:


对我来说,我试图通过回调(在另一个窗口中)访问/设置angular的值;解决方案是使用jQuery的
数据
方法。
data
API允许您将对象文本附加到DOM节点。因此,我没有求助于全局变量(这也会起作用),而是做了如下事情:

/**
 * Listen to a global "lookupTemplate" event
 */
$j(document).on("lookupTemplate", function(event, template) {
        $j(document).data("template", template);
        window.open("/callbackURL", 'Callback Window', 'status=0,toolbar=0,height=230,width=358');
    });

/**
* Callback from other window, to change template ID and name (it's global)
* @param {String} template_id the ID for the selected template from the other window
* @param {String} template_name the name for the selected template from the other window
*/
function templateChosen(template_id, template_name) {
    var template = $(document).data("template");
    var appendedSelector = "[" + template.index + "]";
    $('[name="templateId' + appendedSelector + '"').val(template_id);
    $('[name="templateName' + appendedSelector + '"').val(template_name);
    // Update angular vars just in case
    template.id = template_id;
    template.name = template_name;
}

var app = angular.module("app", []).controller("ActionsController", function($scope) {
        $scope.actions = [];
        $scope.lookupTemplate = function(index) {
            $scope.actions[index].template.index = index;
            $(document).trigger("lookupTemplate", $scope.actions[index].template);
        }
    }
);

在这里,我使用包含在Angular中的
{index}}
帮助程序增加每个新
操作的
name
属性。

确实有效!虽然发生了一些意外情况:函数运行,但如果函数访问作用域的一个表达式,它不会在控制器中更新。此外,是否有任何原因会在函数中调用时返回
未捕获引用错误:$未定义
?页面中是否有jQuery?如果没有jQuery,尝试angular.element(document.getElementById('#myDiv')).scope().info('me')。对于非jquery示例,getElementById('#myDiv')中不应存在哈希
angular.element($('#myDiv')).scope().info('me')
angular.element(document.getElementById('myDiv')).scope().info('me')
angular.element(document.getElementById('myDiv')).scope().$apply();
/**
 * Listen to a global "lookupTemplate" event
 */
$j(document).on("lookupTemplate", function(event, template) {
        $j(document).data("template", template);
        window.open("/callbackURL", 'Callback Window', 'status=0,toolbar=0,height=230,width=358');
    });

/**
* Callback from other window, to change template ID and name (it's global)
* @param {String} template_id the ID for the selected template from the other window
* @param {String} template_name the name for the selected template from the other window
*/
function templateChosen(template_id, template_name) {
    var template = $(document).data("template");
    var appendedSelector = "[" + template.index + "]";
    $('[name="templateId' + appendedSelector + '"').val(template_id);
    $('[name="templateName' + appendedSelector + '"').val(template_name);
    // Update angular vars just in case
    template.id = template_id;
    template.name = template_name;
}

var app = angular.module("app", []).controller("ActionsController", function($scope) {
        $scope.actions = [];
        $scope.lookupTemplate = function(index) {
            $scope.actions[index].template.index = index;
            $(document).trigger("lookupTemplate", $scope.actions[index].template);
        }
    }
);