Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/388.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_Asynchronous_Byref - Fatal编程技术网

Javascript 如何在AngularJS中将数据异步从模型返回到控制器

Javascript 如何在AngularJS中将数据异步从模型返回到控制器,javascript,angularjs,asynchronous,byref,Javascript,Angularjs,Asynchronous,Byref,我刚接触AngularJS,尝试制作一个MVC应用程序,控制器可以连接到同一类型的多个模型 因此: 我创建了一个连接到测试模型的控制器,以异步获取信息,如: function TestController($scope, Test) { $scope.model = {}; $scope.load : function(id) { Test.get($scope, id); } } 该模型使用http协议从服务器检索(json)信息。模型如下所示

我刚接触AngularJS,尝试制作一个MVC应用程序,控制器可以连接到同一类型的多个模型

因此:

我创建了一个连接到测试模型的控制器,以异步获取信息,如:

function TestController($scope, Test)
{
    $scope.model = {};  

    $scope.load : function(id) {
         Test.get($scope, id);
    }
}
该模型使用http协议从服务器检索(json)信息。模型如下所示:

myApp.factory('Test',function($http) {
    get : function(variable, id) {
        $http({
           url: 'api/load/'+id
        }).success(function(response) {
           variable.model = response;       
        });
     } 
});
在那里,“模型”的名称硬连接到控制器中。所以没有办法加载 第二个测试模型,在控制器中,因为现有模型将被重写

如果我更改行:

    Test.get($scope, id);

而模型

     variable = response;
角度停止的魔力。控制器中的模型未更新。没有byRef 在Javascript中


是否有一种变通方法,使模型可以在一个控制器中多次使用

好吧,你不需要像这样呼叫服务。首先,$http调用返回承诺,可以使用“then”回调来处理。因此,您可以为类似的调用添加多个不同的回调。就你而言:

myApp.factory('Test',function($http) {
    get : function(id) {
        return $http({
            url: 'api/load/'+id
        });
    } 
});
在控制器中:

function TestController($scope, Test) {
    $scope.model = {};  

    $scope.load : function(id) {
        Test.get(id).then(function(result) {
             $scope.var1 = result;
        });

        Test.get(id).then(function(result) {
             $scope.var2 = result;
        });
    }
}
function TestController($scope, Test) {
    $scope.model = {};  

    $scope.load : function(id) {
        Test.get($scope, 'var1', id);
        Test.get($scope, 'var2', id);
    }
}
另一种方法是这样做:

myApp.factory('Test',function($http) {
    get : function(context, variable, id) {
        return $http({
            url: 'api/load/'+id
        }).success(function(result) {
            context[variable] = result;
        });
    } 
});
在控制器中:

function TestController($scope, Test) {
    $scope.model = {};  

    $scope.load : function(id) {
        Test.get(id).then(function(result) {
             $scope.var1 = result;
        });

        Test.get(id).then(function(result) {
             $scope.var2 = result;
        });
    }
}
function TestController($scope, Test) {
    $scope.model = {};  

    $scope.load : function(id) {
        Test.get($scope, 'var1', id);
        Test.get($scope, 'var2', id);
    }
}

谢谢,这就是我要找的。让它更具可读性。