Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/431.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AngularJS将数据传递给视图_Javascript_Angularjs - Fatal编程技术网

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保存在某个父控制器中,并初始化该值以检查该值是否存在。我已经修改了代码。