$scope在Controller中没有定义

$scope is undefined in Controller

本文关键字:定义 Controller scope      更新时间:2023-09-26

我有世界上最简单的控制器,我想访问$scope,但它是"未定义的",我不能,对于我的生活工作出为什么,但所有的函数都被正确调用,DataService等工作完美,只是我没有访问$scope?!

控制器代码在

下面
app = angular.module("windscreens", []);
app.controller('DamageCtrl', function($scope, DataService) {
  $scope.setDamageLocation = function(location) {
    DataService.getDamage().setLocation(location);
  };
  $scope.isLocation = function(requiredLocation) {
    return DataService.getDamage().getLocation() === requiredLocation;
  };
  $scope.progress = function() {
  };
}); 

要从HTML模板中访问作用域中的属性,你只需要使用属性名,不需要用它来写$scope

的例子:

<button ng-click="progress()"></button>

在你的javascript中,你只能访问控制器及其函数中的$作用域。如果你调用一个外部资源,例如:DataService模块,它将无法访问$作用域,除非你将它作为参数显式传递。

我设法使用替代语法使其工作。如下所述,仍然不确定为什么不工作,但他们哼

app.controller('DamageCtrl', ['$scope', 'DataService',
  function($scope, DataService) {
    $scope.setDamageLocation = function(location) {
      DataService.getDamage().setLocation(location);
    };
    $scope.isLocation = function(requiredLocation) {
      return DataService.getDamage().getLocation() === requiredLocation;
    };
    $scope.progress = function() {
      console.log($scope);
    };
  }
]);