gpt4 book ai didi

javascript - Angular 范围未定义

转载 作者:行者123 更新时间:2023-12-03 09:21:42 27 4
gpt4 key购买 nike

我有一个 Angular Controller ,我正在将其设置为使用“controller as”语法:

angular.module('app', []).controller('ctrl1', ctrl1);

ctrl1.$inject = ['$http', '$compile'];

function ctrl1($http, $compile) {
function someHttpGet() {
//get data
var compiled = $compile(data)($scope);
}
}

我什至尝试将编译后的数据绑定(bind)到作用域对象的原因是因为我看到了一个说要这样做的答案,但这对我来说没有意义,因为在我看来,我正在使用 Controller 作为语法.

我怎样才能让它正常工作?

最佳答案

您没有在 Controller 中注入(inject) $scope:

ctrl1.$inject = ['$http', '$compile', '$scope'];//like others $scope needs to be injected
function ctrl1($http, $compile, $scope) {
function someHttpGet() {
//get data
var compiled = $compile(data)($scope);
}
}

因为 $scope 不会自动注入(inject) Controller 中。

关于javascript - Angular 范围未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31823204/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com