最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - console.log not working inside angular controller - Stack Overflow

programmeradmin0浏览0评论

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?

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.

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?

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.

Share Improve this question asked Mar 28, 2015 at 19:40 divakardivakar 1,3796 gold badges15 silver badges31 bronze badges 4
  • please provide your html code as well – Manube Commented Mar 28, 2015 at 19:44
  • 1 Are you adding ng-app and ng-controller on your html? Do you see any console.log at all? Any javascript errors? – Tiago Martins Commented Mar 28, 2015 at 20:11
  • Impossible to answer without knowing your HTML or your ng-route setup. – Bill Bergquist Commented Mar 29, 2015 at 1:30
  • Hi Awal, i am able to figure it out. i forgot it, since it was long time before. However, i have the github repo which you may take a look now. github./divakarvenu/FoodRec/tree/master/assets/js/Controller/… – divakar Commented Feb 2, 2016 at 17:21
Add a ment  | 

2 Answers 2

Reset to default 0

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

Code

foodshareModule.controller('scratchListController', function($scope, $state, Food) { //you were added Notes, but there should be Food
    console.log("working");
    //..other code here
})
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 working in the console.

发布评论

评论列表(0)

  1. 暂无评论