[英]How to pass the value into other controller?
我认为这些信息不足以完全理解,但我只是对此提供了一些指导。 或者如果您愿意,我可以与您共享完整的代码
我正在尝试将此数组从API放入卖方数组。
this.fetchSellers = function () {
var date = new Date().toISOString();
$http.get('/api/sellers', {params: {auctionDate: date}})
.success(function (sellers) {
sellers && sellers.length && ($scope.sellers = sellers);
});
};
我试图将值Seller.symbol之一放入下面的数组列表中。
$scope.sellers = []
它能够像这样加载到ng-option
<select class="form-control" ng-model="auction.seller" ng-options="o.id as o.symbol for o in sellers" required></select>
HTML代码
<tbody class="auction-group" ng-repeat="a in auctions">
<tr>
@*auction *@
<td>{{ acv('auctionDate', a) }}</td>
@*seller*@
<td>{{ a.seller }}</td>
ng代码
this.tableComplexFieldDict = {
auctionDate: {
label: 'Auction',
cacheKey: 'auctionDate',
getCacheValue: function (a) {
return moment(a.startAt).format('MM/DD/YY');
}
},
facility: {
label: 'Facility',
cacheKey: 'facility',
getCacheValue: _.property('contract.facility')
},
seller: {
label: 'Seller',
cacheKey: 'seller',
getCacheValue: function (a) {
return a.seller;
}
},
为此,您必须使用其他服务。 理想情况下,您可以在控制器之间传递数据,反之亦然。 因此,只需创建另一个服务并将其包含在您要向其传递数据的控制器中即可。
这是例子
要么
// declare the app with no dependencies var myApp = angular.module('myApp', []); myApp.factory('Data', function(){ return { FirstName: '' }; }); myApp.controller('one', function( $scope, Data ){ $scope.Data = Data; }); myApp.controller('two', function( $scope, Data ){ $scope.Data = Data; });
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>JS Bin</title> <script src="https://rawgit.com/angular/bower-angular/master/angular.min.js"></script> </head> <body> <div ng-app="myApp"> <h3>write something in text box it will be printed by both controller</h3> <div ng-controller="one"> <input type="text" ng-model="Data.FirstName"><!-- Input entered here --> <br>Controller 1 : <strong>{{Data.FirstName}}</strong><!-- Successfully updates here --> </div> <hr> <div ng-controller="two"> Controller 2: {{Data.FirstName}}<!-- How do I automatically updated it here? --> </div> </div> </body> </html>
可以通过3种方式在控制器之间传递数据:
两个控制器都使用带有设置获取数据功能的共享服务
使用通过根范围的直接数据绑定作为介质来存储从中读取的数据
(可怕地侵犯了根范围)
对我来说,最佳实践是使用存储和读取服务;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.