首次应用程序加载时我想计算一个值并将其分配给$scope.about
app.controller('myCtrl', function ($scope,$http,$location,$route) {
$scope.var1 = 'some value'; /*this works*/
$scope.about = $scope.getAbout(); //ERROR IS ON THIS LINE
/*above line throws error saying $scope.getAbout is not a function*/
$scope.getAbout = function(){
var a = /* get it from webservice via $http */
a = process a;
return a;
}
$scope.otherFunction = function(){
//if I call $scope.getAbout here from inside another function it works fine.
$scope.about = $scope.getAbout();
}
});
答案 0 :(得分:0)
在定义之前使用$scope.getAbout
。首先定义函数,然后使用它。
$scope.about = $scope.getAbout(); // This WILL throw an error
// Define the function
$scope.getAbout = function(){
var a = /* get it from webservice via $http */
a = process a;
return a;
}
$scope.about = $scope.getAbout(); // This WON'T throw an error
答案 1 :(得分:0)
而不是返回函数$ scope.getAbout只是使用它。希望它会奏效。
$scope.getAbout = function(){
var a = /* get it from webservice via $http */
a = process a;
$scope.about = a;
return false;
}
无需使用$ scope.about = $ scope.getAbout();