gpt4 book ai didi

javascript - 引用错误: lecturerFac is not defined

转载 作者:行者123 更新时间:2023-11-28 18:05:35 24 4
gpt4 key购买 nike

当我加载 html 页面时,我的 Controller 从 API 端点检索有关类(class)的数据。该页面会填充有关类(class)的数据。但同时我想用有关类(class)讲师的数据填充页面的一部分(他们的图像、姓名、描述等...)。我使用 ng-init 指令将讲师姓名传递给方法,但我得到了

ReferenceError:讲师Fac 未定义

我不确定,但我相信问题是我使用 ng-init 指令调用 getLecturer() 函数的方式。

我希望在页面加载时将讲师的详细信息与类(class)详细信息一起显示在页面上。

courses.html

<div class="container"  ng-controller="CoursesDetailsCtrl">

<div class="row" >
<div class="col-4" ng-model="getLecturer(courses.lecturer)">
<div>
<h3>{{lecturer.name}}</h3>
<<div>
<img class="img-circle" ng-src="{{lecturer.picture}}" alt="" />
</div>
<p>{{lecturer.description}}</p> -->
</div>

</div>
<div class="col-8">
<div class="myContainer" >
<h2>{{courses.name}}</h2>
<div class="thumbnail">
<img ng-src="{{courses.picture}}" alt="" />
</div>
<div>
<p>{{courses.description}}</p>
</div>
</div>
</div>
</div>
</div>

类(class)详细信息Ctrl

todoApp.controller('CoursesDetailsCtrl', ['coursesFac','lecturerFac','$scope','$stateParams', function CoursesCtrl(coursesFac, lecturerFac, $scope, $stateParams){

$scope.getLecturer = function(name){
lecturerFac.getLecturerByName(name)
.then(function (response) {
$scope.lecturer = response.data;
console.log($scope.lecturer);
}, function (error) {
$scope.status = 'Unable to load lecturer data: ' + error.message;
console.log($scope.status);
});
};

}]);

讲师Fac

todoApp.factory('lecturerFac', ['$http', function($http) {

var urlBase = '/api/lecturer';
var coursesFac = {};

lecturerFac.getLecturer = function () {
return $http.get(urlBase);
};

lecturerFac.getLecturerByName = function (name) {
return $http.get(urlBase + '/' + name);
};

return lecturerFac;
}]);

最佳答案

todoApp.factory('lecturerFac', ['$http', function($http) {

var urlBase = '/api/lecturer';
var coursesFac = {};
var service = {};
service.getLecturer = function () {
return $http.get(urlBase);
};

service.getLecturerByName = function (name) {
return $http.get(urlBase + '/' + name);
};

return service;
}]);

关于javascript - 引用错误: lecturerFac is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42723839/

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