私は自分のコードベースをcomponent-iseにしようとしており、この問題を抱えています。AngularJS Components - バックエンド変数を渡すにはどうすればよいですか?
$スコープとコントローラを使用する場合、ng-initを使用してrestトークメソッドにサーバートークンを渡します。コンポーネントで同じ操作を実行しようとしていません。
javascriptの
angular
.module('myApp', [])
.controller('mainCtrl', function() {
var self = this;
self.options = function() {
var o = {}
o.token = self.serverToken
return o;
}
self.restData = {
url: 'http://rest.url',
options: self.options()
}
})
.component('myComponent', {
bindings: {
restData: '<'
},
template: '<p>template, calls child components</p>',
controller: function(restService) {
this.callRestService = function() {
restService.get(this.restData.url, this.restData.options)
}
console.log(this.restData.url) // http://rest.url
console.log(this.restData.options) // {token: undefined}
}
})
HTML
<html ng-app="myApp">
<!-- head -->
<body ng-controller="mainCtrl as m" ng-init="m.serverToken='12345'">
<my-component rest-data="m.restData"></my-component>
</body>
</html>
にはどうすればコンポーネントに値を渡すのですか?
あなたは 'self.options(使用し、その後、2 essesとself.optionss''としてあなたはセーブ機能) ' 1つの属性で 'options'属性を設定すると、決して設定しない属性の' token'にアクセスしようとします。おそらく 'this.restData.options.token'にアクセスして' ss 'を修正すると助けになるでしょうか? – Duncan
これを指摘してくれてありがとう、いくつかのタイプミスがありました。私は今それらを修正しましたが、問題は残っていますが、問題が残ります – EdwardJPayton
'restData: '<'' to 'restData: '@'' – KTU