AngularJS,我的第一个html组件"widget"
AngularJS, my first html component "widget"
我用yeoman生成了我的第一个AngularJS应用https://github.com/yeoman/generator-angular
我创建了我的第一条路由,目前正在努力在指令上写什么,在控制器上写什么,如何在我从POST或PUT接收的APIService中指定JSON消息(我有一个后端准备好了).
我创建了一个新的服务,并试图扩展一个代码,但我得到的错误:无法读取属性'then'的未定义。服务中的代码为:
angular.module('baApp').service('myService', function ($http, $q) {
this.getTiers = function(){
$http.get('/data/tiers.json').success(function (data) {
console.log('$http tiers');
console.log(data);
});
};
this.getCapabilities = function(){
$http.get('/data/capabilities.json').success(function (data) {
console.log('$http capabilities');
console.log(data);
});
};
return this;
});
这是控制器中的代码:
angular.module('baApp')
.controller('MyappCtrl', function ($scope, myService) {
myService.getTiers().then(function(res){
$scope.tiers = res;
console.log('Tiers');
console.log(res);
});
myService.getCapabilities().then(function(res){
$scope.capabilities = res;
console.log('Capabilities');
console.log(capabilities);
});
});
@Micky,
这可能会帮助您查看
我已经添加了一些示例代码,但如果你需要一些虚拟数据从api直到它准备好,那么你可以使用json来模拟api响应。
这是示例代码
var app = angular.module('myApp', []);
app.controller('ctrl',function($scope, myService){
myService.getData().then(function(res){
$scope.data = res;
});
});
//service here
app.service('myService', function($http, $q){
var responseJson = {'message':'welcome to angular'}
this.getData = function(){
// call api or json file
// like
//return $http.get('api url or /test.json').
// fake response here
var deferred = $q.defer();
deferred.resolve(responseJson);
return deferred.promise;
}
return this;
});
相关文章:
- CKEditor Widget-阻止编辑可编辑元素本身
- 铬:“;未捕获的语法错误:意外的标记:"
- 可以设置“;文件名"发生错误时显示的内联脚本标记的
- JS表单提交"无法使用Chrome数据保护程序加载此页面.尝试重新加载页面.调试信息:POST CISmtuK
- 检测电话窃听,即:<a href="电话:xxx">在UIWebview上
- 使用“+="操作人员
- //而不是在src=“”上使用http://"属性
- "未捕获的语法错误:意外的标记}"
- 可以<脚本类型=“;text/javascript”>window.location=“/"</
- "实例范围”;TypeScript类的getter/setter
- Javascript复选框函数:;缺少:在属性id之后"
- "“;变量未引用正确的对象
- "日期“;AJAX请求返回的类型值未定义
- 在Widget代码中使用javascript从左向右滑动光标图像
- 得到"TypeError:无法读取属性'filename'未定义的“;调用“npm start
- Soundcloud api"未捕获的类型错误:无法读取属性'uri'“未定义”;
- "工具提示"jQuery插件坏了
- "锻造;React中的表达式
- 图像可以从源<img src=""/>.TEXT可以在没有javascript的情况下从外部
- 如何提取“;href"最近列表项中的属性值