[英]Send a data to another controller in AngularJS
How I can to send array.length to another controller?我如何将 array.length 发送到另一个控制器?
First controller: Code below第一个控制器:下面的代码
function uploader_OnAfterAddingFile(item) {
var doc = {item: {file: item.file}};
if (doc.item.file.size > 10240) {
doc.item.file.sizeShort = (Math.round((doc.item.file.size / 1024 / 1024) * 100) / 100) + 'MB';
} else {
doc.item.file.sizeShort = (Math.round((doc.item.file.size / 1024) * 100) / 100) + 'KB';
}
doc.item.showCancel = true;
if ($scope.documentStatus) {
item.formData.push({status: $scope.documentStatus});
}
if ($scope.tenderDraftId) {
item.formData.push({tenderDraftId: $scope.tenderDraftId});
}
item.getDoc = function () { return doc; };
doc.item.getUploadItem = function () { return item; };
$scope.documents.push(doc);
//I need send $scope.documents.length
}
send to this function on other controller Second Controller:发送到其他控制器上的此功能第二个控制器:
They are in one page.它们在一页上。
First Controller it is a component which release upload files.首先是Controller,它是一个发布上传文件的组件。
Second controller it is a modal window where we have 2 input of text and element with first controller.第二个控制器是一个模态窗口,我们在第一个控制器中输入了 2 个文本和元素。
All I need it to now array.length of files which were upload in submit function on modal window.我现在只需要它 array.length 在模式窗口的提交功能中上传的文件。 I tried with $rootScope but it didn`t help me.我试过 $rootScope 但它没有帮助我。
I think what you really want to do here is to $emit
or $broadcast
an event.我认为您在这里真正想做的是$emit
或$broadcast
一个事件。 This will allow you to write less code and be able to pass this data effortlessly to anyplace in the application that you wish, Using event listeners, $on
, would also provide the same effect.这将允许您编写更少的代码并能够毫不费力地将此数据传递到您希望的应用程序中的任何位置,使用事件侦听器$on
也将提供相同的效果。
Please give this article a good read to understand which option is best for your use case.请仔细阅读本文以了解哪个选项最适合您的用例。 https://medium.com/@shihab1511/communication-between-controllers-in-angularjs-using-broadcast-emit-and-on-6f3ff2b0239d https://medium.com/@shihab1511/communication-between-controllers-in-angularjs-using-broadcast-emit-and-on-6f3ff2b0239d
TLDR: $rootScope.$broadcast vs. $scope.$emit TLDR: $rootScope.$broadcast 与 $scope.$emit
You could create a custom service that stores and returns the value that you need: see more information under the title 'Create Your Own Service'.您可以创建一个自定义服务来存储和返回您需要的值:请参阅“创建您自己的服务”标题下的更多信息。
Or you could inject routeParams to the second controller: see more information或者您可以将 routeParams 注入第二个控制器:查看更多信息
I came across a similar problem the other day.前几天我遇到了类似的问题。 I would use data binding along with a $ctrl method.我会使用数据绑定和 $ctrl 方法。 Here is a really good article with an example that you can replicate with your use case: http://dfsq.info/site/read/angular-components-communication Hope this helps.这是一篇非常好的文章,其中包含一个示例,您可以将其复制到您的用例中:http: //dfsq.info/site/read/angular-components-communication希望这会有所帮助。 This form of communication makes it a lot easier to share data between two components on the same page.这种通信形式使得在同一页面上的两个组件之间共享数据变得容易得多。 Article example:文章示例:
.component('headerComponent', {
template: `
<h3>Header component</h3>
<a ng-class="{'btn-primary': $ctrl.view === 'list'}" ng-click="$ctrl.setView('list')">List</a>
<a ng-class="{'btn-primary': $ctrl.view === 'table'}" ng-click="$ctrl.setView('table')">Table</a>
`,
controller: function( ) {
this.setView = function(view) {
this.view = view
this.onViewChange({$event: {view: view}})
}
},
bindings: {
view: '<',
onViewChange: '&'
}
})
So it means that header component can be used something like this所以这意味着标题组件可以像这样使用
<header-component view="root.view" on-view-change="root.view = $event.view"></header-component>
.component('mainComponent', {
template: `
<h4>Main component</h4>
Main view: {{ $ctrl.view }}
`,
bindings: {
view: '<'
}
})
<header-component view="root.view" on-view-change="root.view = $event.view"></header-component>
<main-component view="root.view"></main-component>
I used the method explained above to pass data between two controllers to hide one component, when a button is clicked in a different component.当在不同组件中单击按钮时,我使用上面解释的方法在两个控制器之间传递数据以隐藏一个组件。 The data that was being passed was a boolean.传递的数据是布尔值。 I would expect you would be able to do the same thing with array.length.我希望你能用 array.length 做同样的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.