console.log无法在角度控制器内部工作

时间:2015-03-28 19:40:15

标签: javascript angularjs

您好我一直试图进入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'
    }
  });
});

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

2 个答案:

答案 0 :(得分:0)

通过查看代码,您似乎在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
})

答案 1 :(得分:0)

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