【问题标题】:Angular js $scope is UndefinedAngular js $scope 未定义
【发布时间】:2016-05-11 06:37:23
【问题描述】:

我在我的一个项目中使用 angular js。我对角度非常陌生,以下错误让我对使用角度更加困惑。

我有一个js函数

function ShowHideEditOptions(id) {
            var editOptions = document.getElementById("editOptions");
            editOptions.style.display = "block";
            selectedNodeId = id;

 InitializeMapForSelectedNode(); 

        }

我想在我的ShowHideEditOptions(id){} 函数中调用InitializeMapForSelectedNode();angular 函数。

有可能吗???

当我这样做时,它给了我一个错误提示 $scope is undefined

这是我的视角控制器

<script>
    var app = angular.module('app', ['kendo.directives']);
    var selectedNodeId = "";
    app.controller("locationController", function ($compile, $scope, $log, $timeout) {    

$scope.InitializeMapForSelectedNode = function () {
                InitializeMapForSelectedNode($scope);
            }


    });
</script>

角度函数是

function InitializeMapForSelectedNode($scope) {

    var locations = $scope.MeterList; // this is where the error occur

    .....
}

这个函数使用了之前绑定的 $scope.MeterList 变量。这是 fire bug 检测到 $scope is Undefined 错误的地方。

【问题讨论】:

  • 是的,有可能,但不推荐,除非你有充分的理由。
  • 您没有将参数$scope 的任何值传递给函数function InitializeMapForSelectedNode($scope),因此它将是undefined 是。
  • 你能在 plunker 上复制同样的内容吗,plnkr.co

标签: javascript .net angularjs angularjs-scope


【解决方案1】:

尝试拨打$scope

<script>
var app = angular.module('app', ['kendo.directives']);
var selectedNodeId = "";
app.controller("locationController", function ($compile, $scope, $log, $timeout) {    

$scope.InitializeMapForSelectedNode = function () {
            $scope.InitializeMapForSelectedNode();
        }


});

【讨论】:

  • 他也可以试试while(true) { }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-11
  • 1970-01-01
  • 1970-01-01
  • 2015-07-23
  • 1970-01-01
相关资源
最近更新 更多