【发布时间】:2017-06-15 14:39:50
【问题描述】:
很遗憾,我无法在组件之间传递数据。 我参加了 bcqr-reader 演示 https://embed.plnkr.co/m9dtF9llcAw7eYE94b5k/preview 并将扫描的信息传递给我的第二个组件。 首先你可以看到我的扫描组件。 “$scope.url”是我感兴趣的对象。
angular.module('foo').component('scan', {
bindings:{url:"="},
templateUrl: 'templates/scan.html',
controller:function($scope){
$scope.start = function() {
$scope.cameraRequested = true;
}
$scope.processURLfromQR = function (url) {
$scope.url = url;
$scope.cameraRequested = false;
}
}
});
我的第二个组件“show”是“$scope.url”的目的地。 稍后我将需要 $scope.url 将其用作 couchdb 查询的 id。 我读了很多,关于组件之间的数据交换,但我没有让我运行。我还尝试将对象存储为 $rootScope。我更喜欢像 $onChanges 这样的基于事件的解决方案,但现在欢迎所有帮助。
angular.module('foo').component('show', {
template: '<p> Transfered value: {{$ctrl.url}}</p>',
bindings:{url:"="},
require: { url:'^scan'
},
controller: function () {
this.$onInit = function () {
console.log(this.foo.url); //
}; }
});
我的 app.js 文件如下所示:
myApp.config(function($stateProvider,$urlRouterProvider) {
var scanState = {
name: 'scan',
url: '/scan',
component:'scan'
}
var showState = {
name:'show',
url: '/show',
component:'show'
}
$stateProvider.state(scanState);
$stateProvider.state(showState);
$urlRouterProvider.otherwise("scan");
});
【问题讨论】:
标签: angularjs angular-ui-router angular-components