如果我像这样声明1常量:
app.constant('appHelper', {
first: 'Firstname',
last: 'Lastname'
});
然后尝试在第二个常量中使用它,如下所示:
app.constant('appHelper2', {
fullName: appHelper.first + '' + appHelper.last
});
不起作用。 Error: appHelper is undefined.
任何想法我该怎么做?
答案 0 :(得分:2)
不确定
var appHelper = {
first: 'Firstname',
last: 'Lastname'
}
app.constant('appHelper', appHelper);
app.constant('appHelper2', {
fullName: appHelper.first + '' + appHelper.last
});
答案 1 :(得分:0)
参考和学分:
is there a way in Angularjs to define constants with other constants?
http://michalostruszka.pl/blog/2012/12/23/angular-better-constant-values/
基于此,以下内容可能适合您。
var myApp = angular.module("exampleApp",[]);
myApp.constant('HELPER', (function() {
// Define your variable
var appHelper = {
first: 'Firstname',
last: 'Lastname'
};
// Use the variable in your constants
return {
appHelper:appHelper,
fullName: appHelper.first + '' + appHelper.last
}
})());
myApp.controller("ExampleCtrl", function(HELPER){
$scope.firstname = HELPER.appHelper.firstname;
$scope.fullName = HELPER.fullName;
});