我刚刚开始,angular.js
今天我正在编写我的控制器:
myApp.controller('RepetitionController', ['$scope', '$location', 'repetitionService',
function ($scope, $location, repetitionService) {
$scope.questions = repetitionService.getQuestions();
$scope.questionsLeft = $scope.questions.length;
$scope.questionsAnswered = 0;
$scope.percentageLeft = ($scope.questionsLeft == 0 ? 100 : 0);
$scope.repetitonState = ???
$scope.endRepetition = function () {
repetitionService.clearSelectedSets();
$location.path("/setsAndCollections");
}
$scope.submitAnswer = function () {
alert("alert");
}
}]);
我开始怀疑。
你可以看到我使用三元运算符来创建初始状态$scope
,现在在我的repetitionState
领域中我想要这样的东西(questionsLeft === 0 ? 'finished' : questions[0].type)
。
有什么方法可以定义一个在填充$scope
对象后调用的函数,某种后构造函数?
或者也许有一种方法可以“观察”函数,所以我可以写
$scope.repetitionState = function(){
///logic here
};
我只是担心在某些情况下我需要编写logicalExpression ? anonymousFunction () : someOtherAnonymousFunction()
,对我来说,嵌套所有这些匿名函数(目前)有点难以阅读,我想知道其中的某些部分angular
是否有用在这种情况下。