Javascript AngularJS将数据传递给视图
我有一个控制器,如下所示:Javascript AngularJS将数据传递给视图,javascript,angularjs,Javascript,Angularjs,我有一个控制器,如下所示: angular.module('myApp').controller('CoursesCtrl', function($scope, $auth, $location, toastr, Course, $http) { $scope.selectedCourse = []; $scope.getCourse = function(id){ Course.getCourse(id) .then(function(respo
angular.module('myApp').controller('CoursesCtrl', function($scope, $auth, $location, toastr, Course, $http) {
$scope.selectedCourse = [];
$scope.getCourse = function(id){
Course.getCourse(id)
.then(function(response) {
$scope.selectedCourse = {
course_name : response.data.data['name'],
course_code : response.data.data['code']
}
$location.path('/course');
})
.catch(function(response) {
toastr.error(response.data.message, response.status);
});
};
});
<div class="ten wide column" ng-controller="CoursesCtrl">
<h2 class="ui header">
{{selectedCourse.course_name}}
</h2>
<h4 class="ui header">
Instructor: {{selectedCourse.course_code}}
</h4>
</div>
意见如下:
angular.module('myApp').controller('CoursesCtrl', function($scope, $auth, $location, toastr, Course, $http) {
$scope.selectedCourse = [];
$scope.getCourse = function(id){
Course.getCourse(id)
.then(function(response) {
$scope.selectedCourse = {
course_name : response.data.data['name'],
course_code : response.data.data['code']
}
$location.path('/course');
})
.catch(function(response) {
toastr.error(response.data.message, response.status);
});
};
});
<div class="ten wide column" ng-controller="CoursesCtrl">
<h2 class="ui header">
{{selectedCourse.course_name}}
</h2>
<h4 class="ui header">
Instructor: {{selectedCourse.course_code}}
</h4>
</div>
{{selectedCourse.course\u name}
讲师:{{selectedCourse.course\u code}
我无法将数据传递到具有位置路径的视图中:课程:
仔细查看代码,是否有遗漏或需要考虑的内容。因为您在
getCourse
函数中为selectedCourse
编写了代码,所以您必须在getCourse
函数之外编写selectedCourse
。在您的code$scope中。selectedCourse
声明为数组。为您的目的将其声明为对象,如$scope.selectedCourse={}代码>
angular.module('myApp').controller('CoursesCtrl', function($scope, $auth, $location, toastr, Course, $http) {
$scope.selectedCourse ={}; //here was the mistake
$scope.getCourse = function(id){
Course.getCourse(id)
.then(function(response) {
$scope.selectedCourse = {
course_name : response.data.data['name'],
course_code : response.data.data['code']
}
$location.path('/course');
})
.catch(function(response) {
toastr.error(response.data.message, response.status);
});
};
});
angular.module('myApp').controller('CoursesCtrl', function ($scope, $auth, $location, toastr, Course, $http) {
//Loading the value based on the existance in parent controller.
$scope.selectedCourse = ($scope.$parent.selectedCourseData)? $scope.$parent.selectedCourseData: {};
$scope.getCourse = function (id) {
Course.getCourse(id)
.then(function (response) {
$scope.selectedCourse = {
course_name: response.data.data['name'],
course_code: response.data.data['code']
}
//Keeping the value in a parent controller
$scope.$parent.selectedCourseData = $scope.selectedCourse;
$location.path('/course');
})
.catch(function (response) {
toastr.error(response.data.message, response.status);
});
};
});
尝试以上答案仍然没有显示数据。是否要将数据传递到“课程”路径?请在发布之前使用搜索功能…@Nitheesh,是的,这就是目的。控制器中是否有其他$scope.selectedCourse初始化?是,因此,问题是$scope的默认定义。selectedCourse将覆盖新定义。因此,最好将$scope.selectedCourse保存在某个父控制器中,并初始化该值以检查该值是否存在。我已经修改了代码。