简体   繁体   English

当在某处使用$ http时,AngularJS范围变量不起作用

[英]AngularJS scope variables not working when using $http somewhere

I have a problem with my $scope variable whenever I try to use $http.get (no matter if I do it in the controller or in a service). 每当我尝试使用$http.get时,我的$scope变量都会出现问题(无论是在控制器中还是在服务中进行操作)。

My controller looks like this: 我的控制器如下所示:

app.controller('HomeController', ['$scope', function($scope, $http) {
    $scope.today = new Date();
}]);

Then I get a proper output of 6.11.2015 for the today variable for example. 然后,例如,对于今天变量,我得到了6.11.2015的正确输出。 But as soon as I put in this or use a service: 但是,一旦我输入或使用一项服务:

app.controller('HomeController', ['$scope', function($scope, $http) {
    $scope.today = new Date();
    $http.get("data/selectAll.php")
    .success(function(data) {
        $scope.lokale = data;
    });
}]);

Then it doesn't work anymore and I get {{today | date : 'dd.MM.yyyy'}} 然后它不再起作用了,我得到了{{today | date : 'dd.MM.yyyy'}} {{today | date : 'dd.MM.yyyy'}} only. {{today | date : 'dd.MM.yyyy'}}{{today | date : 'dd.MM.yyyy'}} The $scope.lokale variable doesn't work either. $scope.lokale变量也不起作用。 I tried to use a json file (that I know of for sure that it works) to make sure that my php file isn't the problem, and it didn't work either, so that doesn't seem like the problem here. 我试图使用一个json文件(我确定可以正常工作)来确保我的php文件不是问题,并且它也不起作用,所以在这里看起来好像不是问题。

The proper way to use $http is 使用$ http的正确方法是

$http.get('/someUrl', config).then(successCallback, errorCallback);

Use .then() instead of .success() 使用.then()代替.success()

Your syntax have a little mistake. 您的语法有一点错误。 :) :)

app.controller('HomeController', ['$scope', '$http', function($scope, $http) {

You've forgoten to add $http as string before the controller function. 您已经放弃在控制器函数之前将$ http作为字符串添加。

Take a look at this Angular Doc about Dependency Injection . 看一下有关依赖注入的 Angular Doc Look for 'A Note on Minification'. 寻找“关于缩小的说明”。

A Note on Minification Since Angular infers the controller's dependencies from the names of arguments to the controller's constructor function, if you were to minify the JavaScript code for PhoneListCtrl controller, all of its function arguments would be minified as well, and the dependency injector would not be able to identify services correctly. 关于缩小的说明由于Angular从参数名称推断出控制器的依赖关系到控制器的构造函数,因此,如果要缩小PhoneListCtrl控制器的JavaScript代码,则其所有函数参数也将被缩小,而依赖项注入器将不会能够正确识别服务。

We can overcome this problem by annotating the function with the names of the dependencies, provided as strings, which will not get minified. 我们可以通过用依赖项的名称注释该函数来克服此问题,这些依赖项的名称以字符串形式提供,不会被最小化。 There are two ways to provide these injection annotations: 提供这些注入注释的方法有两种:

Create a $inject property on the controller function which holds an array of strings. 在包含字符串数组的控制器函数上创建一个$ inject属性。 Each string in the array is the name of the service to inject for the corresponding parameter. 数组中的每个字符串都是要为相应参数注入的服务的名称。 In our example we would write: 在我们的示例中,我们将编写:

 function PhoneListCtrl($scope, $http) {...}` PhoneListCtrl.$inject = ['$scope', '$http']; phonecatApp.controller('PhoneListCtrl', PhoneListCtrl); 

Use an inline annotation where, instead of just providing the function, you provide an array. 使用内联注释,您可以在其中提供一个数组,而不仅仅是提供函数。 This array contains a list of the service names, followed by the function itself. 该数组包含服务名称列表,其后是函数本身。

function PhoneListCtrl($scope, $http) {...} phonecatApp.controller('PhoneListCtrl', ['$scope', '$http', PhoneListCtrl]); 函数PhoneListCtrl($ scope,$ http){...} phonecatApp.controller('PhoneListCtrl',['$ scope','$ http',PhoneListCtrl]); Both of these methods work with any function that can be injected by Angular, so it's up to your project's style guide to decide which one you use. 这两种方法都可以与Angular可以注入的任何函数一起使用,因此取决于项目的样式指南来决定使用哪个函数。

When using the second method, it is common to provide the constructor function inline as an anonymous function when registering the controller: 使用第二种方法时,通常在注册控制器时将内联构造函数作为匿名函数提供:

phonecatApp.controller('PhoneListCtrl', ['$scope', '$http', function($scope, $http) {...}]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM