gpt4 book ai didi

angularjs - 将解析与 angularjs 组件一起使用

转载 作者:行者123 更新时间:2023-12-04 03:57:00 25 4
gpt4 key购买 nike

我正在尝试在呈现页面之前解析客户列表。

这是状态提供者引用,我有解决方法。

angular.module('app')
.config(($stateProvider) => {
$stateProvider
.state('customers', {
url: '/customers',
template: '<customers></customers>',
resolve: {
test: function () {
return 'nihao';
},
},
});
});

其次是组件,它应该从resolve中调用#test。它应该做的就是在控制台上打印“nihao”这个词。
(function myCustomersConfig() {
class MyCustomersComponent {
constructor(test) {
this.test = test;
console.log(this.test);
}

angular.module('app').component('myCustomers', {
templateUrl: 'app/customers/customers.html',
controller: MyCustomersComponent,
});
}());

但是,我不断收到此错误:
angular.js:13708 Error: [$injector:unpr] Unknown provider: testProvider <- test
http://errors.angularjs.org/1.5.7/$injector/unpr?p0=testProvider%20%3C-%20test
at angular.js:68
at angular.js:4502
at Object.getService [as get] (angular.js:4655)
at angular.js:4507
at getService (angular.js:4655)
at injectionArgs (angular.js:4679)
at Object.invoke (angular.js:4701)
at $controllerInit (angular.js:10234)
at nodeLinkFn (angular.js:9147)
at angular.js:9553

我可以看到它正在运行解析函数,所以它可以工作,但它不会注入(inject)方法!有任何想法吗?

最佳答案

您的代码缺少属性和绑定(bind),以便解析工作。

angular.module('app')
...
template: '<customers test="$resolve.test"></customers>',
resolve: { test: function () { return {value: 'nihao'}; } },
...
});

(function myCustomersConfig() {

function MyCustomersComponent {
// You can use test right away, and also view as $ctrl.test
console.log(this.test);
}

angular.module('app')
.component('myCustomers', {
templateUrl: 'app/customers/customers.html',
controller: MyCustomersComponent,
bindings: {
test: "<",
}
});
}());

关于angularjs - 将解析与 angularjs 组件一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38510620/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com