Angular:使用$ resource后$ scope和模板中断

时间:2013-06-11 18:51:48

标签: javascript angularjs

我还没有构建实际的应用程序,但我正在尝试使用Angular来处理HTTP请求。

这是我的小(和坏)HTML模板:

<div class="row-fluid" ng-controller="SearchCtrl">
    <div>zip: {{zip}}</div>
    <div>id: {{appid}}</div>
    <div>key: {{appkey}}</div>
    <div>JSON: {{jsonz}}</div>
</div>

这是我特定页面的控制器:

  controller('SearchCtrl', ['$scope','$routeParams',function($scope,$routeParams){
      $scope.appid = *;
      $scope.appkey = *;
      if($routeParams.zipcode) {
        $scope.zip = $routeParams.zipcode;
        $scope.jsonz = $resource('api/:act', {act:'search',appid:$scope.appId,appKey:$scope.appkey,zipCode:$scope.zip}).query();
      }
  }])

我认为该模块已正确初始化。

angular.module('myApp.controllers', ['ngResource'])

当$ resource行未被注释时,HTML将按字面显示,而不会从Angular替换模板。注释掉,zip / id / key被正确替换。

我知道我的资源问题可能不对,但我不知道我应该做些什么。

UPD: angular-resource.js中似乎有一些未定义的值,我将尝试调查/重新下载angularjs。

[15:44:57.157] Error: value is undefined
ResourceFactory/extractParams/<@file:///C:/demo/app/lib/angular/angular-resource.js:344
forEach@file:///C:/demo/app/lib/angular/angular.js:137
extractParams@file:///C:/demo/app/lib/angular/angular-resource.js:343
ResourceFactory/</Resource[name]@file:///C:/demo/app/lib/angular/angular-resource.js:398
@file:///C:/demo/app/js/controllers.js:79
invoke@file:///C:/demo/app/lib/angular/angular.js:2902
instantiate@file:///C:/demo/app/lib/angular/angular.js:2914
@file:///C:/demo/app/lib/angular/angular.js:4805
update@file:///C:/demo/app/lib/angular/angular.js:14198
Scope.prototype.$broadcast@file:///C:/demo/app/lib/angular/angular.js:8307
updateRoute/<@file:///C:/demo/app/lib/angular/angular.js:7463
qFactory/defer/deferred.promise.then/wrappedCallback@file:///C:/demo/app/lib/angular/angular.js:6846
qFactory/defer/deferred.promise.then/wrappedCallback@file:///C:/demo/app/lib/angular/angular.js:6846
qFactory/ref/<.then/<@file:///C:/demo/app/lib/angular/angular.js:6883
Scope.prototype.$eval@file:///C:/demo/app/lib/angular/angular.js:8057
Scope.prototype.$digest@file:///C:/demo/app/lib/angular/angular.js:7922
Scope.prototype.$apply@file:///C:/demo/app/lib/angular/angular.js:8143
done@file:///C:/demo/app/lib/angular/angular.js:9170
completeRequest@file:///C:/demo/app/lib/angular/angular.js:9333
createHttpBackend/</xhr.onreadystatechange@file:///C:/demo/app/lib/angular/angular.js:9304

1 个答案:

答案 0 :(得分:2)

您必须在控制器中注入$ resource:

controller('SearchCtrl', ['$scope', '$routeParams', '$resource', 
function($scope, $routeParams, $resource){ ...