我收到了一个名为
的错误参数'myController'不是函数,未定义
这是我的代码:
脚本
var app = angular.module("app", []);
app.service("serviceText", function () {
this.start = function (text) {
return "service is started, Text is " + text;
};
this.end = function (text) {
return "service is stopped, Text is " + text;
};
});
function myController($scope,serviceText) {
$scope.call = function () {
$scope.msg = serviceText.start($scope.txt);
}
$scope.stop = function () {
$scope.msg = serviceText.end($scope.txt);
}
}
HTML
<body ng-app="app" >
<div ng-controller="myController">
<input type="text" name="name" ng-model="txt" />
<button ng-click="call()" >Start</button>
<button ng-click="stop()" >Stop</button>
<br />
<div>{{msg}}</div>
</div>
</body>
请告诉我我的代码中有什么错误
答案 0 :(得分:2)
如果你使用1.3以上的角度版本,你应该像这样声明控制器,
app.controller('myController', function($scope, serviceText){
});
如果角度版本低于1.3,则应调用控制器,
app.controller('myController', myController)
答案 1 :(得分:0)
只需在控制器功能定义之前添加app.controller('myController', myController)
答案 2 :(得分:0)
您需要告诉AngularJS
哪个function
是controller
添加此行
app.controller("myController",myController);
您的代码应如下所示
var app = angular.module("app", []);
app.service("serviceText", function () {
this.start = function (text) {
return "service is started, Text is " + text;
};
this.end = function (text) {
return "service is stopped, Text is " + text;
};
});
app.controller("myController",myController);
function myController($scope,serviceText) {
$scope.call = function () {
$scope.msg = serviceText.start($scope.txt);
}
$scope.stop = function () {
$scope.msg = serviceText.end($scope.txt);
}
}
https://dl.dropboxusercontent.com/u/23002394/ONVIF-Emulator-20160529.tar.xz
答案 3 :(得分:0)
You must have to add the controller's constructor function to the module using the .controller() like
var app = angular.module("app", []);
app.controller('myController',myController )
app.service("serviceText", function () {
this.start = function (text) {
return "service is started, Text is " + text;
};
this.end = function (text) {
return "service is stopped, Text is " + text;
};
});
function myController($scope,serviceText) {
$scope.call = function () {
$scope.msg = serviceText.start($scope.txt);
}
$scope.stop = function () {
$scope.msg = serviceText.end($scope.txt);
}
}
答案 4 :(得分:0)
// create your main module
angular.module('myModule', []);
// create a service on this module
angular.module('myModule').factory('xxxService', xxxService);
xxxService.$inject = ['$q', 'httpService'];
function xxxService($q, httpService) {
var service = {
x: x
};
return service;
function x() {
}
}
// create a controller on this module
angular.module('myModule').controller('XXXController', XXXController);
XXXController.$inject = [];
function XXXController() {
var vm = this;
activate();
function activate() {
}
}