ngController 中的错误 (angularJS)
error in ngController (angularJS)
我收到错误消息
Argument 'myController' is not a function, got undefined
这是我的代码:
脚本
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>
请告诉我我的代码有什么问题
只需在您的控制器函数定义前添加 app.controller('myController', myController)
如果你使用的是angular 1.3以上的版本,你应该这样声明控制器,
app.controller('myController', function($scope, serviceText){
});
如果你的 angular 版本低于 1.3 ,你应该调用控制器,
app.controller('myController', myController)
你需要告诉 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);
}
}
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);
}
}
// 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() {
}
}
我收到错误消息
Argument 'myController' is not a function, got undefined
这是我的代码:
脚本
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>
请告诉我我的代码有什么问题
只需在您的控制器函数定义前添加 app.controller('myController', myController)
如果你使用的是angular 1.3以上的版本,你应该这样声明控制器,
app.controller('myController', function($scope, serviceText){
});
如果你的 angular 版本低于 1.3 ,你应该调用控制器,
app.controller('myController', myController)
你需要告诉 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);
}
}
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);
}
}
// 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() {
}
}