Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.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
Angularjs 使用工厂将数据返回控制器时出错_Angularjs - Fatal编程技术网

Angularjs 使用工厂将数据返回控制器时出错

Angularjs 使用工厂将数据返回控制器时出错,angularjs,Angularjs,我是angular的新手,我正在使用服务从json文件获取数据,然后将数据返回给控制器。当我单击该按钮时,控制器方法不会执行,并且console.log中没有错误。我错过了什么 我的服务代码: Service.js app.factory('MovieService', function ($http) { var url = "js/data.json"; var data; return {

我是angular的新手,我正在使用服务从json文件获取数据,然后将数据返回给控制器。当我单击该按钮时,控制器方法不会执行,并且console.log中没有错误。我错过了什么

我的服务代码:

Service.js

 app.factory('MovieService', function ($http) {
            var url = "js/data.json";
            var data;

            return {
                getData: function() {
                    return $http.get(url)
                        .success(function(response) {
                            data = response;
                            return data;

                        })
                        .error(function (error){
                            console.log("error");
                        })
                }
            };
        });
app.controller('MainController1', ['$scope', '$log','$location','MovieService', function($scope,$log,$location,MovieService) {

    console.log("click");
    var getData = function() {
            // This service's function returns a promise
            MovieService.getData()
                .then(function(data) {
                    // promise fulfilled
                   console.log("controller data");

                    $scope.custdata = data;
                    console.log($scope.custdata);

                }, function(error) {
                    // promise rejected, could log the error with: 
                    console.log("error");
                });
        };    


}]) 
Controller.js

 app.factory('MovieService', function ($http) {
            var url = "js/data.json";
            var data;

            return {
                getData: function() {
                    return $http.get(url)
                        .success(function(response) {
                            data = response;
                            return data;

                        })
                        .error(function (error){
                            console.log("error");
                        })
                }
            };
        });
app.controller('MainController1', ['$scope', '$log','$location','MovieService', function($scope,$log,$location,MovieService) {

    console.log("click");
    var getData = function() {
            // This service's function returns a promise
            MovieService.getData()
                .then(function(data) {
                    // promise fulfilled
                   console.log("controller data");

                    $scope.custdata = data;
                    console.log($scope.custdata);

                }, function(error) {
                    // promise rejected, could log the error with: 
                    console.log("error");
                });
        };    


}]) 
index.html

<div class="main" ng-controller="MainController1 as main">

        <input type="button" name="getdata" value ="Get data" ng-click="main.getData ()"></input>

    </div>

您需要在作用域上绑定控制器函数

$scope.getData = function() { }"
反而

var getData = function() { }
在模板中调用它,就像

ng-click="getData ()"

您需要在作用域上绑定控制器函数

$scope.getData = function() { }"
反而

var getData = function() { }
在模板中调用它,就像

ng-click="getData ()"

您正在使用
控制器作为别名
语法

在这种情况下,需要从视图访问的控制器功能应分配给

因此,将函数定义为
this
的一个属性,而不是一个独立的函数-如下所示:

this.getData = function () {...}

您使用的是
var getData
,这将使函数成为本地函数,而不是公开它。

您使用的是
控制器作为别名的语法

在这种情况下,需要从视图访问的控制器功能应分配给

因此,将函数定义为
this
的一个属性,而不是一个独立的函数-如下所示:

this.getData = function () {...}

您使用的是
var getData
,这将使函数成为本地函数,而不是公开它。

应该注意以下几点:-

1)您应该使用该函数而不是var将函数绑定到
controller中的控制器,语法如下:-

this.getData=function(){//您的逻辑}

2)首先在
success()
error()
中包装promise两次,然后在另一个
then()
函数中包装promise,方法如下:-

在职:-

 getData: function() {
                    return $http.get(url);
                        
                } 
在控制器中:-

 MovieService.getData()
                .then(function(response) {
                    // promise fulfilled
                   console.log("controller data");

                    $scope.custdata = response.data;
                    console.log($scope.custdata);

                }, function(error) {
                    // promise rejected, could log the error with: 
                    console.log("error");
                });
3)
不应关闭,因为它没有关闭标记

希望有帮助:)


应该注意以下几点:-

1)您应该使用该函数而不是var将函数绑定到
controller中的控制器,语法如下:-

this.getData=function(){//您的逻辑}

2)首先在
success()
error()
中包装promise两次,然后在另一个
then()
函数中包装promise,方法如下:-

在职:-

 getData: function() {
                    return $http.get(url);
                        
                } 
在控制器中:-

 MovieService.getData()
                .then(function(response) {
                    // promise fulfilled
                   console.log("controller data");

                    $scope.custdata = response.data;
                    console.log($scope.custdata);

                }, function(error) {
                    // promise rejected, could log the error with: 
                    console.log("error");
                });
3)
不应关闭,因为它没有关闭标记

希望有帮助:)


控制器方法应绑定到
this
like
this。getData
控制器方法应绑定到
this
like
this。getData
在运行plunker时,我收到错误:plunker正确运行为什么在end@squirod,我想过滤响应数据。例如,我想根据id进行筛选,然后返回数据。。。我是否必须为此编写单独的javascript函数,或者在angular中是否有更简单的方法。例如,对于基于movieid=1的过滤器,不需要单独的函数,只需在then()中迭代数据,并根据所需的ID将其推送到$scope.custdata中即可。我在服务angular.forEach(response){//code}但是在运行plunker时抛出了一个错误我得到了一个错误:plunker运行正确,为什么在end@squirod,我想筛选响应数据。例如,我想根据id进行筛选,然后返回数据。。。我是否必须为此编写单独的javascript函数,或者在angular中是否有更简单的方法。例如,基于movieid=1的过滤器不需要单独的函数,只需在then()中迭代数据,并根据需要的ID将其推送到$scope.custdata中。我在服务angular.forEach(response){//code}中尝试了类似的操作,但抛出了一个错误