AngularJS |没有错误,但我的依赖注入无效

时间:2017-05-28 04:47:12

标签: javascript angularjs dependency-injection

我还没有找到问题的答案。

总结一下,我试图通过在我的控制器中&中访问的' directives.js' 文件中完成我的指令来完成依赖注入。 #39; controllers.js' 文件。

即使在我的' controllers.js' 文件中,当我在模块中添加方括号时也没有其他内容,



(function () {
	'use strict';

	angular
		.module('blogApp', ['file-directives'])
		// Career Controller
		.controller('CareerCtrl', function ($scope, $http, $location, $routeParams) {
			$scope.getCareers = function () {
				$http.get('/career').then(function (response) {
					$scope.careers = response.data;
				});
			};
		
			$scope.getCareer = function () {
				var id = $routeParams.id;
				$http.get('/career/' + id).then(function (response) {
					$scope.career = response.data;
				});
			};
		
			$scope.addCareer = function () {
				$http.post('/career', $scope.career).then(function (response) {
					window.location.href = '#!/career';
				});
			};

			$scope.updateCareer = function () {
				var id = $routeParams.id;
				$http.put('/career/' + id, $scope.career).then(function (response) {
					window.location.href = '#!/career';
				});
			};

			$scope.removeCareer = function (id) {
				$http.delete('/career/' + id).then(function (response) {
					window.location.href = '#!/career';
				});
			};
		})
		
		// Lifestyle Controller
		.controller('LifestyleCtrl', function ($scope, $http, $location, $routeParams) {
			$scope.getLifestyles = function () {
				$http.get('/lifestyle').then(function (response) {
					$scope.lifestyles = response.data;
				});
			};

			$scope.getLifestyle = function () {
				var id = $routeParams.id;
				$http.get('/lifestyle/' + id).then(function (response) {
					$scope.lifestyle = response.data;
				});
			};

			$scope.addLifestyle = function () {
				$http.post('/lifestyle', $scope.lifestyle).then(function (response) {
					window.location.href = '#!/lifestyle';
				});
			};

			$scope.updateLifestyle = function () {
				var id = $routeParams.id;
				$http.put('/lifestyle/' + id, $scope.lifestyle).then(function (response) {
					window.location.href = '#!/lifestyle';
				});
			};

			$scope.removeLifestyle = function (id) {
				$http.delete('/lifestyle/' + id).then(function (response) {
					window.location.href = '#!/lifestyle';
				});
			};
		})
	
		// Travel Controller
		.controller('TravelCtrl', function ($scope, $http, $location, $routeParams) {
			$scope.getTravels = function () {
				$http.get('/travel').then(function (response) {
					$scope.travels = response.data;
				});
			};

			$scope.getTravel = function () {
				var id = $routeParams.id;
				$http.get('/travel/' + id).then(function (response) {
					$scope.travel = response.data;
				});
			};

			$scope.addTravel = function () {
				$http.post('/travel', $scope.travel).then(function (response) {
					window.location.href = '#!/travel';
				});
			};

			$scope.updateTravel = function () {
				var id = $routeParams.id;
				$http.put('/travel/' + id, $scope.travel).then(function (response) {
					window.location.href = '#!/travel';
				});
			};

			$scope.removeTravel = function (id) {
				$http.delete('/travel/' + id).then(function (response) {
					window.location.href = '#!/travel';
				});
			};
		})
	
		// Main Page Controller
		.controller('MainCtrl', function ($scope, $http, $location, $routeParams) {
			$scope.getCareers = function () {
				$http.get('/career').then(function (response) {
					$scope.careers = response.data;
				});
			};

			$scope.getLifestyles = function () {
				$http.get('/lifestyle').then(function (response) {
					$scope.lifestyles = response.data;
				});
			};

			$scope.getTravels = function () {
				$http.get('/travel').then(function (response) {
					$scope.travels = response.data;
				});
			};
		});
}());



(function () {
  'use strict';

  angular
    .module('file-directives', [])
    .directive('fileModel', ['$parse', function ($parse) {
      return {
        restrict: 'A',
        link: function (scope, element, attrs) {
          var parsedFile = $parse(attrs.fileModel);
          var parsedFileSetter = parsedFile.assign;
          
          element.bind('change', function () {
            scope.apply(function () {
              parsedFileSetter(scope, element[0].files[0]);
            });
          });
        }
      };
    }]);
}());

<!DOCTYPE html>
<html lang="en" ng-app="blogApp">

	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
		<title>Shelby Cherie</title>

		<!-- Bootstrap -->
		<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
		<link rel="stylesheet" href="css/main.css">
		<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
		<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
		<!--[if lt IE 9]>
	<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
	<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
	<![endif]-->
	</head>

	<body>

		<header>
			<nav class="navbar navbar-default">
				<div class="container-fluid">
					<div class="navbar-header">
						<a class="navbar-brand" href="#!/">Shelby Cherie</a>
					</div>
					<ul class="nav navbar-nav navbar-right">
						<li><a href="#"><span class="glyphicon glyphicon-user"></span> Sign Up</a></li>
						<li><a href="#"><span class="glyphicon glyphicon-log-in"></span> Login</a></li>
					</ul>
				</div>
			</nav>
		</header>

		<div id="main">
			<div class="container">
				<div class="row">
					<div class="col-md-3">
						<aside>
							<ul class="nav nav-pills nav-stacked side-menu">
								<li><a href="#!/career">career</a></li>
								<li><a href="#!/lifestyle">lifestyle</a></li>
								<li><a href="#!/travel">travel</a></li>
								<li><a href="#!/about">about me</a></li>
							</ul>
						</aside>
					</div>
						<div ng-view></div>
				</div>
			</div>
		</div>

		<footer>
			<div class="container">
				<div class="social-media text-center">
					<ul class="list-inline">
						<li><span class="glyphicon glyphicon-user" aria-hidden="true"></span></li>
						<li><span class="glyphicon glyphicon-user" aria-hidden="true"></span></li>
						<li><span class="glyphicon glyphicon-user" aria-hidden="true"></span></li>
					</ul>
				</div>
				<div class="sub-footer">
					<p>Website Developed by <a href="#">Derek Hawkins Design</a></p>
				</div> 
			</div>
		</footer>


		<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
		<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
		<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.js"></script>
		<script src="https://code.angularjs.org/1.6.4/angular-route.js"></script>
		<script src="https://code.angularjs.org/1.6.4/angular-parse-ext.js"></script>
		<script type="text/javascript" src="routes/routes.js"></script>
		<script type="text/javascript" src="controllers/controllers.js"></script>
		<script type="text/javascript" src="directives/directives.js"></script>
		<!-- Include all compiled plugins (below), or include individual files as needed -->
		<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
	</body>

</html>
&#13;
&#13;
&#13;

我的网页将完全空白,没有错误。

controllers.js

   angular
    .module('blogApp', ['file-directives'])
    // Career Controller
    .controller('CareerCtrl', function ($scope, $http, $location, $routeParams) {
        $scope.getCareers = function () {
            $http.get('/career').then(function (response) {
                $scope.careers = response.data;
            });
        };

directives.js

    angular
     .module('file-directives')
     .directive('fileModel', ['$parse', function ($parse) {
       return {
         restrict: 'A',
         link: function (scope, element, attrs) {
         var parsedFile = $parse(attrs.fileModel);
         var parsedFileSetter = parsedFile.assign;

         element.bind('change', function () {
          scope.apply(function () {
            parsedFileSetter(scope, element[0].files[0]);
        });
      });
     }
    };
   }]);
}());

2 个答案:

答案 0 :(得分:0)

我认为您无法在controller.js中加载ngRoute模块

<强> controller.js

angular.module('blogApp', ['ngRoute','file-directives'])
// Career Controller
.controller('CareerCtrl',function ($scope, $http, $location, $routeParams) {
    $scope.getCareers = function () {
        $http.get('/career').then(function (response) {
            $scope.careers = response.data;
        });
    }
}); 

<强> directives.js

angular.module('file-directives',[])
     .directive('fileModel', ['$parse', function ($parse) {
       return {
         restrict: 'A',
         link: function (scope, element, attrs) {
         var parsedFile = $parse(attrs.fileModel);
         var parsedFileSetter = parsedFile.assign;

          element.bind('change', function () {
                  scope.apply(function () {
                        parsedFileSetter(scope, element[0].files[0]);
                  });
          });
        }
      };
  }]);

答案 1 :(得分:0)

在声明&#34;文件指令&#34;

时,您错过了方括号
 angular
     .module('file-directives',[])