AngularJS http请求从不执行
AngularJS http request is never executed
我在这里有一个最小的复数。
发生了什么:
- 成功发出初始
$http
请求 - 单击事件绑定到指令中的按钮
- 单击该按钮可启动所需的功能
- 该功能中的
$http
请求(与步骤1中的请求相同)未激发
因为代码很短,我也会把它贴在这里。
模板
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<title>AngularJS Plunker</title>
<!-- angular source -->
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
<p>Click this button and an http request should log to the console</p>
<button make-request act='flip()'>Get Gaius</button>
</body>
</html>
控制器
app = angular.module('plunker', [])
app.controller 'MainCtrl', ($scope, $http) ->
# this function is just here to show that no errors are thrown
err = (err) -> console.log 'err', err
# this successfully gets
$http.get('gaius.json')
.then ((res) -> console.log 'init data', res.data), err
$scope.flip = ->
# although this function is called,
console.log 'called to act'
# http does not get. No request is made.
$http.get('gaius.json')
.then ((res) -> console.log 'flip data', res.data), err
app.directive 'makeRequest', ($compile) ->
scope:
act: '&'
link: (scope, element, attrs) ->
element.bind 'click', (e) -> scope.act()
数据
{
"name": "gaius baltar"
}
知道为什么请求没有执行吗?
您必须通过在作用域上调用$apply()来传播promise解析。
app.directive 'makeRequest', ($compile) ->
scope:
act: '&'
link: (scope, element, attrs) ->
element.bind 'click', (e) -> scope.act(); scope.$apply();
相关文章:
- 防止双击执行两次jQuery post请求
- 暂停函数执行流程,直到ajax请求完成
- Chrome浏览器“;挂起”;同时在循环中执行AJAX请求
- 如何仅在完成对gap.client.youtube的请求.execute后执行函数
- AJAX 请求不会在 400 个错误查询上执行失败
- 嵌套在 .get 请求中的 jQuery 不会执行
- 通过 ajax 请求执行 ajax 请求
- 如何在执行 Ajax 请求大量数据库更新时实现进度条
- 从 Web Worker 执行 AJAX 请求是否可行
- 在不执行ajax请求的情况下,使用jQuery读取HTTP标头以检查404
- Angular在不触发摘要的情况下执行XHR请求
- 按顺序执行ajax请求的动态编号
- 根据条件线性执行请求
- Firefox SDK 多次执行请求
- 如何将 Google 地方自动完成功能配置为仅在输入字段中至少有 n 个字母时执行请求
- 执行请求数组失败,并多次调用Error: done().映射请求数组不会触发请求
- HTTP403: FORBIDDEN——服务器理解请求,但拒绝执行请求
- 使用AngularJS'执行请求的顺序;s ng资源
- 按顺序在循环中执行请求
- 表单没有'不能正确执行请求后功能