AngularJS对模板没有约束力

AngularJS对模板没有约束力,angularjs,angularjs-service,Angularjs,Angularjs Service,我的服务: getAll: function(url) { deferred.resolve($http({ <--- deferred.resolve should not have been here method: 'GET', url: url, contentType: "application/json; charset=utf-8",

我的服务:

        getAll: function(url) {
         deferred.resolve($http({  <--- deferred.resolve should not have been here
                method: 'GET',
                url: url,
                contentType: "application/json; charset=utf-8",
                cache: true,
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' }                    
            }));
            return deferred.promise;
        },
        getAll: function (url) {
            $http({
                method: 'GET',
                url: url,
                contentType: "application/json; charset=utf-8",
                cache: true,
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
            }).success(function (data) {
                deferred.resolve(data);
            });
            return deferred.promise;
     // $scope.PassengerVehicles = crudSvc.getAll('/_json/PassengerVehicles.js');
     // should have been:
      crudSvc.getAll('/_json/PassengerVehicles.js').then(function (data) {
        $scope.PassengerVehicles = data;
    });
       <ul data-ng-repeat="passVeh in PassengerVehicles">
          <li>{{passVeh.itemId}}</li>
        </ul>

这是造成我错误的2个原因中的第1个:

        getAll: function(url) {
         deferred.resolve($http({  <--- deferred.resolve should not have been here
                method: 'GET',
                url: url,
                contentType: "application/json; charset=utf-8",
                cache: true,
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' }                    
            }));
            return deferred.promise;
        },
        getAll: function (url) {
            $http({
                method: 'GET',
                url: url,
                contentType: "application/json; charset=utf-8",
                cache: true,
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
            }).success(function (data) {
                deferred.resolve(data);
            });
            return deferred.promise;
     // $scope.PassengerVehicles = crudSvc.getAll('/_json/PassengerVehicles.js');
     // should have been:
      crudSvc.getAll('/_json/PassengerVehicles.js').then(function (data) {
        $scope.PassengerVehicles = data;
    });
       <ul data-ng-repeat="passVeh in PassengerVehicles">
          <li>{{passVeh.itemId}}</li>
        </ul>

我的控制器:

        getAll: function(url) {
         deferred.resolve($http({  <--- deferred.resolve should not have been here
                method: 'GET',
                url: url,
                contentType: "application/json; charset=utf-8",
                cache: true,
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' }                    
            }));
            return deferred.promise;
        },
        getAll: function (url) {
            $http({
                method: 'GET',
                url: url,
                contentType: "application/json; charset=utf-8",
                cache: true,
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
            }).success(function (data) {
                deferred.resolve(data);
            });
            return deferred.promise;
     // $scope.PassengerVehicles = crudSvc.getAll('/_json/PassengerVehicles.js');
     // should have been:
      crudSvc.getAll('/_json/PassengerVehicles.js').then(function (data) {
        $scope.PassengerVehicles = data;
    });
       <ul data-ng-repeat="passVeh in PassengerVehicles">
          <li>{{passVeh.itemId}}</li>
        </ul>
这是造成我错误的2个原因中的第2个:

        getAll: function(url) {
         deferred.resolve($http({  <--- deferred.resolve should not have been here
                method: 'GET',
                url: url,
                contentType: "application/json; charset=utf-8",
                cache: true,
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' }                    
            }));
            return deferred.promise;
        },
        getAll: function (url) {
            $http({
                method: 'GET',
                url: url,
                contentType: "application/json; charset=utf-8",
                cache: true,
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
            }).success(function (data) {
                deferred.resolve(data);
            });
            return deferred.promise;
     // $scope.PassengerVehicles = crudSvc.getAll('/_json/PassengerVehicles.js');
     // should have been:
      crudSvc.getAll('/_json/PassengerVehicles.js').then(function (data) {
        $scope.PassengerVehicles = data;
    });
       <ul data-ng-repeat="passVeh in PassengerVehicles">
          <li>{{passVeh.itemId}}</li>
        </ul>
我的模板:

        getAll: function(url) {
         deferred.resolve($http({  <--- deferred.resolve should not have been here
                method: 'GET',
                url: url,
                contentType: "application/json; charset=utf-8",
                cache: true,
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' }                    
            }));
            return deferred.promise;
        },
        getAll: function (url) {
            $http({
                method: 'GET',
                url: url,
                contentType: "application/json; charset=utf-8",
                cache: true,
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
            }).success(function (data) {
                deferred.resolve(data);
            });
            return deferred.promise;
     // $scope.PassengerVehicles = crudSvc.getAll('/_json/PassengerVehicles.js');
     // should have been:
      crudSvc.getAll('/_json/PassengerVehicles.js').then(function (data) {
        $scope.PassengerVehicles = data;
    });
       <ul data-ng-repeat="passVeh in PassengerVehicles">
          <li>{{passVeh.itemId}}</li>
        </ul>
  • {{passVeh.itemId}
这是我的扑救,已经更正:

        getAll: function(url) {
         deferred.resolve($http({  <--- deferred.resolve should not have been here
                method: 'GET',
                url: url,
                contentType: "application/json; charset=utf-8",
                cache: true,
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' }                    
            }));
            return deferred.promise;
        },
        getAll: function (url) {
            $http({
                method: 'GET',
                url: url,
                contentType: "application/json; charset=utf-8",
                cache: true,
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
            }).success(function (data) {
                deferred.resolve(data);
            });
            return deferred.promise;
     // $scope.PassengerVehicles = crudSvc.getAll('/_json/PassengerVehicles.js');
     // should have been:
      crudSvc.getAll('/_json/PassengerVehicles.js').then(function (data) {
        $scope.PassengerVehicles = data;
    });
       <ul data-ng-repeat="passVeh in PassengerVehicles">
          <li>{{passVeh.itemId}}</li>
        </ul>

比你第四

您拥有
getAll()
函数,可以立即解析并向控制器返回承诺。您需要允许
getAll()
函数在解决以下问题时执行并处理承诺:

getAll: function(url) {
            return $http({
                method: 'GET',
                url: url,
                contentType: "application/json; charset=utf-8",
                cache: true,
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' }                    
            });
        },
在控制器中:

    crudSvc.getAll('/_json/PassengerVehicles.js').success(function (vehicles){
        $scope.PassengerVehicles = vehicles;
      });

不同之处在于,您返回的是一个立即解决并返回另一个承诺的承诺。您只需解决http承诺。

我将服务更改为完全属于您的服务。我的控制器方法调用与您的完全相同,控制台输出与以前一样,我仍然没有绑定到模板。您是否将控制器更改为与我的一样,因为以前没有。调试服务调用以确保其返回承诺,并调试控制器以确保正在调用“then”函数。对服务函数进行了轻微更改,它将解决您的问题。请重新检查我的答案,注意我现在返回的是
response.data
而不是
response
。这也没用,你看到我的plunker了吗?我看到了,我道歉,因为我不太熟悉直接使用$http。这是一个正在工作的plunker,我更新了我的答案: