angularjs – 角度组件绑定未定义
发布时间:2020-12-17 10:18:15 所属栏目:安全 来源:网络整理
导读:我正在尝试用ngRoute组合我的第一个角度组件,到目前为止,我无法获得要解决的数据. 配置: .when('/myfirstcomponent',{ template: 'myfirstcomponent claimKeys="$resolve.claimKeys"/myfirstcomponent',resolve: { claimKeys: ['$http',function($http) { $
我正在尝试用ngRoute组合我的第一个角度组件,到目前为止,我无法获得要解决的数据.
配置: .when('/myfirstcomponent',{ template: '<myfirstcomponent claimKeys="$resolve.claimKeys"></myfirstcomponent>',resolve: { claimKeys: ['$http',function($http) { $http.get('server/claimkeys.json').then((response) => { var claimKeys = response.data.DATASET.TABLE; return claimKeys; }); }] } }) 零件: .component('myfirstcomponent',{ bindings: { 'claimKeys': '@' },templateUrl: 'components/component.html',controller: [function() { this.$onInit = function() { var vm = this; console.log(vm.claimKeys); }; }] 组件的html只有一个带有一些随机文本的p元素. 我可以看到调试时我正在检索数据,但我无法在组件控制器上访问它… 编辑:感谢下面接受的答案,我解决了我的问题.它与异步调用的问题没有任何关系,但与我如何定义我的路由和组件有关.请参阅以下代码进行修复.再次感谢.
一些问题:
>正如你所说的声明指令中的键应该是声明键 app.component('myfirstcomponent',{ bindings: { 'claimKeys': '=' },template: 'components/component.html',controller: function() { var vm = this; this.$onInit = function() { console.log(vm.claimKeys); }; } }); app.config(function ($stateProvider) { $stateProvider.state('myfirstcomponent',{ url: '/myfirstcomponent',template: '<myfirstcomponent claim-keys="$resolve.claimKeys"></myfirstcomponent>',resolve: { claimKeys: ['$http',function($http) { return $http.get('claimkeys.json').then((response) => { return response.data.DATASET.TABLE; }); }] },controller: function (claimKeys) { this.claimKeys = claimKeys; },controllerAs: '$resolve' }) }); plunker:http://plnkr.co/edit/Nam4D9zGpHvdWaTCYHSL?p=preview,我在这里使用.state而不是.when用于路由. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |