在angularJS里,我把自己写的js文件分别放到app.js,controller.js和
在angularJS里,我把自己写的js文件分别放到app.js,controller.js和service.js(要用的数据)里,请问如何在controller.js用...
在angularJS里,我把自己写的js文件分别放到app.js,controller.js和service.js(要用的数据)里,请问如何在controller.js用那些数据啊?
展开
1个回答
展开全部
你可以查查angularjs 搜依赖注入
我贴个例子
//define a module
var mainApp = angular.module("mainApp", []);
...
//create a service which defines a method square to return square of a number.
mainApp.service('CalcService', function(MathService){
this.square = function(a) {
return MathService.multiply(a,a);
}
});
//inject the service "CalcService" into the controller
mainApp.controller('CalcController', function($scope, CalcService, defaultInput) {
$scope.number = defaultInput;
$scope.result = CalcService.square($scope.number);
$scope.square = function() {
$scope.result = CalcService.square($scope.number);
}
});
就是这样调用service的
我贴个例子
//define a module
var mainApp = angular.module("mainApp", []);
...
//create a service which defines a method square to return square of a number.
mainApp.service('CalcService', function(MathService){
this.square = function(a) {
return MathService.multiply(a,a);
}
});
//inject the service "CalcService" into the controller
mainApp.controller('CalcController', function($scope, CalcService, defaultInput) {
$scope.number = defaultInput;
$scope.result = CalcService.square($scope.number);
$scope.square = function() {
$scope.result = CalcService.square($scope.number);
}
});
就是这样调用service的
追问
module里那个[ ]三个文件都不用写吗
追答
angular.module后的[]里写你要引用的插件,比如ui-router,bootstrap等
关于service和路由文件的引用就是我贴的例子里写的那样
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询