这里我在.constant中定义了一些网址,如
app.constant('UrlConstant',function(){
var GlobalUrl = "http://localhost:5833/";})
这个UrlConstant
如何在我的控制器中使用
var app = angular.module('MyApp', [])
app.controller('twoWayBinding', function ($scope, UrlConstant) {
$scope.changefun = function () {
console.log(UrlConstant.GlobalUrl);
}
在console.log
我得到了不明白的
答案 0 :(得分:2)
您需要将常量声明为
app.constant('UrlConstant','http://localhost:5833/')
<强>样本强>
var app = angular.module('MyApp', [])
app.controller('twoWayBinding', function ($scope, UrlConstant) {
$scope.changefun = function () {
console.log(UrlConstant);
}
});
app.constant('UrlConstant','http://localhost:5833/')
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="MyApp" ng-controller="twoWayBinding">
<button ng-click="changefun()">GetVal</button>
</body>
&#13;
答案 1 :(得分:1)
使用object
作为常量值:
app.constant('UrlConstant', {
GlobalUrl: "http://localhost:5833/"
});
然后您可以使用UrlConstant.GlobalUrl
:
var app = angular.module('MyApp', [])
app.controller('twoWayBinding', function ($scope, UrlConstant) {
$scope.changefun = function () {
console.log(UrlConstant.GlobalUrl);
}
})