在此先感谢,实际上我想从 app.Directive 调用控制器中的函数,请任何人告诉我如何调用?我也将参数传递给该函数?我是 Angular 新手,这里是所有代码。
var app = angular.module('quizApp', []);
app.controller("SaveCtrl", function (scope) {
$scope.Save = function (score) {
$scope.TestDetailsViewModel = {};
$scope.TestDetailsViewModel.CorrectAnswer = $scope.score;
$http({
method: "post",
url: "/Home/ResultSave",
datatype: "json",
data: JSON.stringify($scope.TestDetailsViewModel)
}).then(function (response) {
alert(response.data);
})
};})
app.directive('quiz', function (quizFactory) {
return {
restrict: 'AE',
scope: {},
templateUrl: '/Home/Dashboard',
link: function (scope, elem, attrs) {
scope.getQuestion = function () {
var q = quizFactory.getQuestion(scope.id);
if (q) {
scope.question = q.question;
scope.options = q.options;
scope.answer = q.answer;
scope.answerMode = true;
} else {
scope.quizOver = true;
//Calling function save(); in Controller
//scope.Save(scope.score);
}
};
}
}});