gpt4 book ai didi

javascript - 将 $scope 替换为 "' controller' as"syntax

转载 作者:行者123 更新时间:2023-12-05 09:22:39 25 4
gpt4 key购买 nike

我正在关注 this AngularJS+ASP.NET tutorial他们使用 $scope,但我正在尝试用新语法 controller 替换过时的用法,如以下问题所述:"AngularJs "controller as" syntax - clarification?"

我所做的目前不起作用。该页面在 nextQuestion() 函数内部调用了 $http.get,但 View 保持不变,只是标题 "loading question...".


我的代码:

JS http://pastebin.com/RfngRuZD

var app = angular.module('QuizApp', [])

app.controller('QuizCtrl', ['$http', function ($http) {
this.answered = false;
this.title = "loading question...";
this.options = [];
this.correctAnswer = false;
this.working = false;

this.answer = function () {
return this.correctAnswer ? 'correct' : 'incorrect';
};

// GET
this.nextQuestion = function () {
this.working = true;
this.answered = false;
this.title = "loading question...";
this.options = [];

$http.get('/api/trivia').success(function (data, status, headers, config) {
this.options = data.options;
this.title = data.title;
this.answered = false;
this.working = false;
}).error(function (data, status, headers, config) {
this.title = "Oops... something went wrong.";
this.working = false;
});
};

// POST
this.sendAnswer = function (option) {
this.working = true;
this.answered = true;

$http.post('/api/trivia', { 'questionId': option.questionId, 'optionId': option.id }).success(function (data, status, headers, config) {
this.correctAnswer = (data === "true");
this.working = false;
}).error(function (data, status, headers, config) {
this.title = "Oops... something went wrong.";
this.working = false;
});
};
}]);

Index.cshtml http://pastebin.com/YmV1hwcU

@{
ViewBag.Title = "Play";
}

<div id="bodyContainer" ng-app="QuizApp">
<section id="content">
<div class="container">
<div class="row">
<div class="flip-container text-center col-md-12" ng-controller="QuizCtrl as quiz" ng-init="quiz.nextQuestion()">
<div class="back" ng-class="{flip: quiz.answered, correct: quiz.correctAnswer, incorrect: !quiz.correctAnswer}">

<p class="lead">{{quiz.answer()}}</p>
<p>
<button class="btn btn-info btn-lg next option" ng-click="quiz.nextQuestion()" ng-disabled="quiz.working">Next Question</button>
</p>
</div>
<div class="front" ng-class="{flip: quiz.answered}">
<p class="lead">{{quiz.title}}</p>
<div class="row text-center">
<button class="btn btn-info btn-lg option"
ng-repeat="option in quiz.options" ng-click="quiz.sendAnswer(option)" ng-disabled="quiz.working">
{{option.title}}
</button>
</div>
</div>
</div>
</div>
</div>
</section>
</div>

@section scripts{
@Scripts.Render("~/Scripts/angular.js")
@Scripts.Render("~/Scripts/app/quiz-controller.js")
}

最佳答案

您基本上找到了为什么在 Controller 中使用 this 不是一个好主意的原因。$http 的成功 promise 中的 this 不是 Controller 的 this,因为此函数在不同的上下文中执行。如果您通过闭包获得 $scope,这将不是问题。

您可以通过定义变量 var that = this; 然后使用 that 来解决这个问题。

关于javascript - 将 $scope 替换为 "' controller' as"syntax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25405543/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com