javascript console.log 在角度控制器内不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29321698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 10:22:51  来源:igfitidea点击:

console.log not working inside angular controller

javascriptangularjs

提问by divakar

Hi i have been trying to get into angular.j. I have tried the following code. But the console.log() not seems to be working. Anything am missing?? like angular concepts or something?

嗨,我一直在尝试进入 angular.j。我试过下面的代码。但是 console.log() 似乎不起作用。有什么遗漏吗??像角度概念之类的?

var foodshareModule= angular.module('food',['ui.router','ngResource']);

console.log("Main file getting included");


foodshareModule.controller("personController", function($scope) {
    console.log($scope);
    $scope.firstName = "John";
    $scope.lastName = "Doe";
    console.log($scope.firstName);
    console.log($scope.lastName);
});

foodshareModule.controller('scratchListController', function($scope,$state,Notes){

   console.log("working");

  $scope.scratchpad =Food.query();

  $scope.deleteScratch = function (scratch,flag) {
    if(flag === 0) {                                            //Checks if Bulk delete or single delete
      if(confirm("You Clicked DELETE !! Are you sure ?")) {
      scratch.$delete(function() {                          //Single delete
        window.location.href = 'http://localhost:1337';
      });
      }
    }
    else {                                                      //Bulk delete
      scratch.$delete(function() {
        window.location.href = 'http://localhost:1337';
      });
    }

  }

  $scope.emptyScratchpad = function () {
    var ask = false;
    if (confirm ("You are about Empty Scratchpad. Sure ????")) {
      ask = true;
    }
    if(ask === true) {
      for (var i = 0; i < $scope.scratchpad.length; i++) {
        $scope.deleteScratch($scope.scratchpad[i],1);
      }
    }

  }
})



foodshareModule.factory('Food', function($resource) {
  return $resource('http://localhost:1337/Food:id', { id: '@_id' }, {
    update: {
      method: 'PUT'
    }
  });
});

Any help would be appreciated. Thanks in advance.

任何帮助,将不胜感激。提前致谢。

回答by Pankaj Parkar

By looking at you code seems like you injected wrong dependency in scratchListController, It should be Foodinstead of Notes

通过查看您的代码,您似乎在 中注入了错误的依赖项scratchListController,它应该是Food而不是Notes

Code

代码

foodshareModule.controller('scratchListController', function($scope, $state, Food) { //you were added Notes, but there should be Food
    console.log("working");
    //..other code here
})

回答by Tirthraj Rao

var app = angular.module('myApp',[])

.factory('Food', function($resource) {
  return $resource('http://localhost:1337/Food:id', { id: '@_id' }, {
    update: {
      method: 'PUT'
    }
  });
})

.controller("personController", function($scope) {
    console.log($scope);
    $scope.firstName = "John";
    $scope.lastName = "Doe";
    console.log($scope.firstName);
    console.log($scope.lastName);
})

.controller('scratchListController', function($scope){

   console.log("working");

  $scope.deleteScratch = function (scratch,flag) {
    if(flag === 0) {                                            //Checks if Bulk delete or single delete
      if(confirm("You Clicked DELETE !! Are you sure ?")) {
      scratch.$delete(function() {                          //Single delete
        window.location.href = 'http://localhost:1337';
      });
      }
    }
    else {                                                      //Bulk delete
      scratch.$delete(function() {
        window.location.href = 'http://localhost:1337';
      });
    }

  }

  $scope.emptyScratchpad = function () {
    var ask = false;
    if (confirm ("You are about Empty Scratchpad. Sure ????")) {
      ask = true;
    }
    if(ask === true) {
      for (var i = 0; i < $scope.scratchpad.length; i++) {
        $scope.deleteScratch($scope.scratchpad[i],1);
      }
    }

  }
});

Here is your solution. It shows workingin the console.

这是您的解决方案。它显示working在控制台中。