Javascript 角度服务';不能使用s函数
我正在尝试使用一个服务,以便为某些ng视图保留一些共同的价值 我在下面有这个script.js和about.html文件(加上一些保存ng视图位置的index.html和一些我认为不相关的其他文件) 我希望,当我按下按钮激活我在服务中定义的set()方法时 但是,当我在chrome上按下此按钮时(在localhost上:....…当然),我在开发人员模式(F12)上收到此错误消息: angular.js:12722 TypeError:myService.set不是函数 为什么??我已经清楚地将它定义为一个函数(通过定义)。 服务返回的对象中是否存在错误 谢谢 script.jsJavascript 角度服务';不能使用s函数,javascript,html,angularjs,Javascript,Html,Angularjs,我正在尝试使用一个服务,以便为某些ng视图保留一些共同的价值 我在下面有这个script.js和about.html文件(加上一些保存ng视图位置的index.html和一些我认为不相关的其他文件) 我希望,当我按下按钮激活我在服务中定义的set()方法时 但是,当我在chrome上按下此按钮时(在localhost上:....…当然),我在开发人员模式(F12)上收到此错误消息: angular.js:12722 TypeError:myService.set不是函数 为什么??我已经清楚地将
var scotchApp = angular.module('scotchApp', ['ngRoute']);
scotchApp.config(function($routeProvider) {
$routeProvider
.when('/', {
templateUrl : 'pages/home.html',
controller : 'SharedController'
})
.when('/about', {
templateUrl : 'pages/about.html',
controller : 'SharedController'
})
.when('/contact', {
templateUrl : 'pages/contact.html',
controller : 'SharedController'
});
});
scotchApp.factory('myService', function() {
var savedData = {}
var set=function (data) {
savedData = data;
}
function get() {
return savedData;
}
return {
set: set,
get: get
}
});
scotchApp.controller('SharedController', ['$scope', '$routeParams', 'myService',
function($scope, myService) {
$scope.updateMsg = function (msg) {
myService.set(msg);
$scope.message = myService.get();
}
}]);
关于.html
<div class="jumbotron text-center">
<h1>old value</h1>
<p>{{ message }}</p>
<button type="button" ng-click="updateMsg('I am in about')">Click Me</button>
</div>
旧价值
{{message}}
点击我
index.html
<!DOCTYPE html>
<html>
<head>
<script data-require="angular.js@1.4.x" src="https://code.angularjs.org/1.4.9/angular.js" data-semver="1.4.9">
</script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular-route.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.3.1/angular-ui-router.min.js"></script>
<script src="script.js"></script>
</head>
<body>
<ul>
<li><a href="#"><i class="fa fa-home"></i> Home</a></li>
<li><a href="#about"><i class="fa fa-shield"></i> About</a></li>
<li><a href="#contact"><i class="fa fa-comment"></i> Contact</a></li>
</ul>
<div ng-app="scotchApp" ng-view>
</div>
</body>
</html>
您的控制器在函数中没有足够的参数。所以不是
scotchApp.controller('SharedController', ['$scope', '$routeParams', 'myService',
function($scope, myService) {
应该是
scotchApp.controller('SharedController', ['$scope', '$routeParams', 'myService',
function($scope, $routeParams, myService)
{
关于它为什么不起作用的更多信息。
Angular在运行时使用数组计算依赖项,以确保所有必需的资源()已加载;它使用函数to之前的数组空格作为要加载的提供程序的列表。DI解析时,它将提供程序按在数组中列出的顺序分配给函数的参数。您的控制器在函数中没有足够的参数。因此
scotchApp.controller('SharedController', ['$scope', '$routeParams', 'myService',
function($scope, myService) {
应该是
scotchApp.controller('SharedController', ['$scope', '$routeParams', 'myService',
function($scope, $routeParams, myService)
{
关于它为什么不起作用的更多信息。
Angular在运行时使用数组计算依赖项,以确保已加载所有必需的资源();它使用函数之前的数组空格作为要加载的提供项列表。DI解析时,它将提供项按数组中列出的顺序分配给函数的参数