Angular服务给出“TypeError:无法读取未定义的属性'helloConsole'”

时间:2022-08-24 13:51:37

I'm studying AngularJS Services and I'm having a problem.

我正在研究AngularJS服务,我遇到了问题。

That's my Angular code:

那是我的Angular代码:

var app = angular.module("todoListApp");

app.controller('MainController', MainController);

MainController.$inject = ['$scope'];

function MainController($scope, dataService){ 
    $scope.helloConsole = dataService.helloConsole;
};

app.service('dataService', function(){
    this.helloConsole = function(){
        console.log("console services");
    };
});
That's my HTML Code

<div ng-controller="MainController" class="list">
<div class="item" ng-class="{'editing-item' : editing, 'edited': todo.edited}" ng-repeat="todo in todos">
        <div class="actions">
            <a href="" ng-click=" editing = !editing">Edit</a>
            <a href="" ng-click="helloConsole()">Save</a>
            <a href="" class="delete">Delete</a>
        </div>
</div> 
</div>

I'm trying to make it so that when I click on Save, the console shows me "console services", but it's giving me an error:

我正试图这样做,以便当我点击保存时,控制台显示“控制台服务”,但它给了我一个错误:

angular.js:13424 TypeError: Cannot read property 'helloConsole' of undefined

angular.js:13424 TypeError:无法读取未定义的属性'helloConsole'

3 个解决方案

#1


4  

Proper Angular Structure

you need to change the way you have written your code. It should look more like this

你需要改变你编写代码的方式。看起来应该更像这样

angular.module("todoListApp", [])

.controller('MainController', ['$scope', 'dataService', function($scope, dataService){

    $scope.helloConsole = dataService.helloConsole;

}])

.service('dataService', [function(){
    this.helloConsole = function(){
        console.log("console services");
    };
}]);

Also this is a 'data service' is this gettig data with a http call? Because if so then make a factory.

这也就是'数据服务'这个带有http调用的gettig数据?因为如果是这样的话就做一个工厂。

  • Controllers for business logic
  • 业务逻辑的控制器
  • Factories for data requests
  • 数据请求的工厂
  • Services for things like login
  • 登录等服务
  • Directives for DOM manipulation
  • DOM操作的指令
  • Filters for format
  • 格式过滤器

#2


2  

Return a singleton service object from angular.service's second function argument. Also, if you're explicit about the dependencies of your controller, thinks will work a lot clearer/better:

从angular.service的第二个函数参数返回单个服务对象。此外,如果您明确了控制器的依赖关系,那么think会更清晰/更好:

var app = angular.module("todoListApp", []);

app.controller('MainController', ['$scope', 'dataService', MainController]);

function MainController($scope, dataService){ 
    $scope.helloConsole = dataService.helloConsole;
    $scope.todos = [{txt:"todo 1"}, {txt:"todo 2"}];
}

app.service('dataService', function(){
    return {
      helloConsole: function(){
        console.log("console services");
      }
    };
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular.js"></script>

<div ng-app="todoListApp">
<div ng-controller="MainController" class="list">
  <div class="item" ng-class="{'editing-item' : editing, 'edited': todo.edited}" ng-repeat="todo in todos">
    {{todo.txt}}
    <div class="actions">
      <a href="" ng-click=" editing = !editing">Edit</a>
      <a href="" ng-click="helloConsole()">Save</a>
      <a href="" class="delete">Delete</a>
    </div>
  </div> 
</div>
</div>

#3


0  

I rewrote this a little bit so it's easier to understand (for me at least). I have added a "todos" array to your code just to make the ng-repeat fire.

我重写了一点,所以它更容易理解(至少对我来说)。我在你的代码中添加了一个“todos”数组,只是为了让ng-repeat起火。

var app = angular.module("todoListApp",[])

.service('dataService', function(){
this.helloConsole = function(){
    console.log("console services");
};
})

.controller('MainController', [ '$scope', 'dataService',function ($scope,dataService) {
 $scope.helloConsole = dataService.helloConsole;
 $scope.todos = [ {
    "todo":"1"
 }
 ]
}])

;

#1


4  

Proper Angular Structure

you need to change the way you have written your code. It should look more like this

你需要改变你编写代码的方式。看起来应该更像这样

angular.module("todoListApp", [])

.controller('MainController', ['$scope', 'dataService', function($scope, dataService){

    $scope.helloConsole = dataService.helloConsole;

}])

.service('dataService', [function(){
    this.helloConsole = function(){
        console.log("console services");
    };
}]);

Also this is a 'data service' is this gettig data with a http call? Because if so then make a factory.

这也就是'数据服务'这个带有http调用的gettig数据?因为如果是这样的话就做一个工厂。

  • Controllers for business logic
  • 业务逻辑的控制器
  • Factories for data requests
  • 数据请求的工厂
  • Services for things like login
  • 登录等服务
  • Directives for DOM manipulation
  • DOM操作的指令
  • Filters for format
  • 格式过滤器

#2


2  

Return a singleton service object from angular.service's second function argument. Also, if you're explicit about the dependencies of your controller, thinks will work a lot clearer/better:

从angular.service的第二个函数参数返回单个服务对象。此外,如果您明确了控制器的依赖关系,那么think会更清晰/更好:

var app = angular.module("todoListApp", []);

app.controller('MainController', ['$scope', 'dataService', MainController]);

function MainController($scope, dataService){ 
    $scope.helloConsole = dataService.helloConsole;
    $scope.todos = [{txt:"todo 1"}, {txt:"todo 2"}];
}

app.service('dataService', function(){
    return {
      helloConsole: function(){
        console.log("console services");
      }
    };
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular.js"></script>

<div ng-app="todoListApp">
<div ng-controller="MainController" class="list">
  <div class="item" ng-class="{'editing-item' : editing, 'edited': todo.edited}" ng-repeat="todo in todos">
    {{todo.txt}}
    <div class="actions">
      <a href="" ng-click=" editing = !editing">Edit</a>
      <a href="" ng-click="helloConsole()">Save</a>
      <a href="" class="delete">Delete</a>
    </div>
  </div> 
</div>
</div>

#3


0  

I rewrote this a little bit so it's easier to understand (for me at least). I have added a "todos" array to your code just to make the ng-repeat fire.

我重写了一点,所以它更容易理解(至少对我来说)。我在你的代码中添加了一个“todos”数组,只是为了让ng-repeat起火。

var app = angular.module("todoListApp",[])

.service('dataService', function(){
this.helloConsole = function(){
    console.log("console services");
};
})

.controller('MainController', [ '$scope', 'dataService',function ($scope,dataService) {
 $scope.helloConsole = dataService.helloConsole;
 $scope.todos = [ {
    "todo":"1"
 }
 ]
}])

;