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

Posted

技术标签:

【中文标题】console.log 在角度控制器中不起作用【英文标题】:console.log not working inside angular controller 【发布时间】:2015-06-02 00:31:08 【问题描述】:

嗨,我一直在尝试进入 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'
    
  );
);

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

【问题讨论】:

请同时提供您的 html 代码 您是否在 html 中添加了 ng-app 和 ng-controller?你看到任何console.log 吗?任何 javascript 错误? 在不知道您的 HTML 或 ng-route 设置的情况下无法回答。 嗨,阿瓦尔,我能弄明白。我忘记了,因为很久以前了。但是,我有 github repo,你现在可以看看。 github.com/divakarvenu/FoodRec/tree/master/assets/js/Controller/… 【参考方案1】:

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

代码

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

【讨论】:

但是我在 personController 上也没有得到任何东西。【参考方案2】:
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);
      
    

  
);

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

【讨论】:

纯代码答案不是很有用。考虑添加一些解释,或评论您的代码。

以上是关于console.log 在角度控制器中不起作用的主要内容,如果未能解决你的问题,请参考以下文章

console.log.apply 在 IE9 中不起作用

Console.log 在异步函数中不起作用

nextjs console.log 在函数返回函数中不起作用

替换方法在浏览器中不起作用但在 console.log 中起作用

Ajax 调用在 angularjs 中不起作用

这些条件在 React 渲染中不起作用 [重复]