Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/361.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.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 $ResourceAPI中的AngularJS示例_Javascript_Angularjs - Fatal编程技术网

Javascript $ResourceAPI中的AngularJS示例

Javascript $ResourceAPI中的AngularJS示例,javascript,angularjs,Javascript,Angularjs,.$资源 在上面的链接中,有一个示例: // Define CreditCard class var CreditCard = $resource('/user/:userId/card/:cardId', {userId:123, cardId:'@id'}, { charge: {method:'POST', params:{charge:true}} }); // We can retrieve a collection from the server var cards = C

.$资源

在上面的链接中,有一个示例:

// Define CreditCard class
var CreditCard = $resource('/user/:userId/card/:cardId',
 {userId:123, cardId:'@id'}, {
  charge: {method:'POST', params:{charge:true}}
 });

// We can retrieve a collection from the server
var cards = CreditCard.query(function() {
  // GET: /user/123/card
  // server returns: [ {id:456, number:'1234', name:'Smith'} ];

  var card = cards[0];
  // each item is an instance of CreditCard
  expect(card instanceof CreditCard).toEqual(true);
  card.name = "J. Smith";
  // non GET methods are mapped onto the instances
  card.$save();
  // POST: /user/123/card/456 {id:456, number:'1234', name:'J. Smith'}
  // server returns: {id:456, number:'1234', name: 'J. Smith'};

  // our custom method is mapped as well.
  card.$charge({amount:9.99});
  // POST: /user/123/card/456?amount=9.99&charge=true {id:456, number:'1234', name:'J. Smith'}
});

// we can create an instance as well
var newCard = new CreditCard({number:'0123'});
newCard.name = "Mike Smith";
newCard.$save();
// POST: /user/123/card {number:'0123', name:'Mike Smith'}
// server returns: {id:789, number:'01234', name: 'Mike Smith'};
expect(newCard.id).toEqual(789);
有一句话:

var card = cards[0];
  • 我不知道数组
    是从哪里来的。我找到的唯一匹配是在前一行,这对我来说没有意义,因为这是函数范围之外的变量

  • 对于jasmine expect函数,Angular是否实际运行它并抛出错误

  • 有些行具有函数
    expect()
    ,例如:

    expect(card instanceof CreditCard).toEqual(true);
    

    我知道这是一个Jasmine测试函数,但我想知道浏览器/Angular是否直接运行它,因为我在代码中没有看到任何Jasmine库。

    回答您的问题1。您引用的行(var card=cards[0];)出现在查询方法的成功回调中。这意味着它已成功执行,这反过来意味着将填充cards变量。cards变量不在范围之外,它可以在回调函数和任何其他函数中引用。通常,您不会在成功回调的范围之外使用cards变量,因为它是一个异步方法


    请详细说明问题2?

    谢谢,你说得对,query()中的函数实际上是一个$http success函数