joh*_*ese 355 javascript angularjs
我正在尝试跨控制器共享数据.用例是一种多步形式,在一个输入中输入的数据稍后用于原始控制器外的多个显示位置.下面的代码和jsfiddle这里.
HTML
<div ng-controller="FirstCtrl">
<input type="text" ng-model="FirstName"><!-- Input entered here -->
<br>Input is : <strong>{{FirstName}}</strong><!-- Successfully updates here -->
</div>
<hr>
<div ng-controller="SecondCtrl">
Input should also be here: {{FirstName}}<!-- How do I automatically updated it here? -->
</div>
Run Code Online (Sandbox Code Playgroud)
JS
// declare the app with no dependencies
var myApp = angular.module('myApp', []);
// make a factory to share data between controllers
myApp.factory('Data', function(){
// I know this doesn't work, but what will?
var FirstName = '';
return FirstName;
});
// Step 1 Controller
myApp.controller('FirstCtrl', function( $scope, Data ){
});
// Step 2 Controller
myApp.controller('SecondCtrl', function( $scope, Data ){
$scope.FirstName = Data.FirstName;
});
Run Code Online (Sandbox Code Playgroud)
任何帮助是极大的赞赏.
tas*_*ATT 473
一个简单的解决方案是让您的工厂返回一个对象,让您的控制器使用对同一对象的引用:
JS:
// declare the app with no dependencies
var myApp = angular.module('myApp', []);
// Create the factory that share the Fact
myApp.factory('Fact', function(){
return { Field: '' };
});
// Two controllers sharing an object that has a string in it
myApp.controller('FirstCtrl', function( $scope, Fact ){
$scope.Alpha = Fact;
});
myApp.controller('SecondCtrl', function( $scope, Fact ){
$scope.Beta = Fact;
});
Run Code Online (Sandbox Code Playgroud)
HTML:
<div ng-controller="FirstCtrl">
<input type="text" ng-model="Alpha.Field">
First {{Alpha.Field}}
</div>
<div ng-controller="SecondCtrl">
<input type="text" ng-model="Beta.Field">
Second {{Beta.Field}}
</div>
Run Code Online (Sandbox Code Playgroud)
演示: http ://jsfiddle.net/HEdJF/
当应用程序变得更大,更复杂且更难测试时,您可能不希望以这种方式从工厂公开整个对象,而是通过getter和setter提供有限的访问权限:
myApp.factory('Data', function () {
var data = {
FirstName: ''
};
return {
getFirstName: function () {
return data.FirstName;
},
setFirstName: function (firstName) {
data.FirstName = firstName;
}
};
});
Run Code Online (Sandbox Code Playgroud)
通过这种方法,消费控制器可以使用新值更新工厂,并观察更改以获取它们:
myApp.controller('FirstCtrl', function ($scope, Data) {
$scope.firstName = '';
$scope.$watch('firstName', function (newValue, oldValue) {
if (newValue !== oldValue) Data.setFirstName(newValue);
});
});
myApp.controller('SecondCtrl', function ($scope, Data) {
$scope.$watch(function () { return Data.getFirstName(); }, function (newValue, oldValue) {
if (newValue !== oldValue) $scope.firstName = newValue;
});
});
Run Code Online (Sandbox Code Playgroud)
HTML:
<div ng-controller="FirstCtrl">
<input type="text" ng-model="firstName">
<br>Input is : <strong>{{firstName}}</strong>
</div>
<hr>
<div ng-controller="SecondCtrl">
Input should also be here: {{firstName}}
</div>
Run Code Online (Sandbox Code Playgroud)
演示: http ://jsfiddle.net/27mk1n1o/
ben*_*ick 71
我不喜欢$watch这样做.您可以只分配数据,而不是将整个服务分配给控制器的范围.
JS:
var myApp = angular.module('myApp', []);
myApp.factory('MyService', function(){
return {
data: {
firstName: '',
lastName: ''
}
// Other methods or objects can go here
};
});
myApp.controller('FirstCtrl', function($scope, MyService){
$scope.data = MyService.data;
});
myApp.controller('SecondCtrl', function($scope, MyService){
$scope.data = MyService.data;
});
Run Code Online (Sandbox Code Playgroud)
HTML:
<div ng-controller="FirstCtrl">
<input type="text" ng-model="data.firstName">
<br>Input is : <strong>{{data.firstName}}</strong>
</div>
<hr>
<div ng-controller="SecondCtrl">
Input should also be here: {{data.firstName}}
</div>
Run Code Online (Sandbox Code Playgroud)
或者,您可以使用直接方法更新服务数据.
JS:
// A new factory with an update method
myApp.factory('MyService', function(){
return {
data: {
firstName: '',
lastName: ''
},
update: function(first, last) {
// Improve this method as needed
this.data.firstName = first;
this.data.lastName = last;
}
};
});
// Your controller can use the service's update method
myApp.controller('SecondCtrl', function($scope, MyService){
$scope.data = MyService.data;
$scope.updateData = function(first, last) {
MyService.update(first, last);
}
});
Run Code Online (Sandbox Code Playgroud)
Ayu*_*hra 11
您可以通过多种方式在控制器之间共享数据
我不会解释,因为已经有人解释过了
运用 $state.go
$state.go('book.name', {Name: 'XYZ'});
// then get parameter out of URL
$state.params.Name;
Run Code Online (Sandbox Code Playgroud)$stateparam以类似的方式工作$state.go,你从发送者控制器传递它作为对象,并使用stateparam收集在接收器控制器中
运用 $rootscope
(a)将数据从子节点发送到父节点控制器
$scope.Save(Obj,function(data) {
$scope.$emit('savedata',data);
//pass the data as the second parameter
});
$scope.$on('savedata',function(event,data) {
//receive the data as second parameter
});
Run Code Online (Sandbox Code Playgroud)
(b)从父母向子女管理人发送数据
$scope.SaveDB(Obj,function(data){
$scope.$broadcast('savedata',data);
});
$scope.SaveDB(Obj,function(data){`enter code here`
$rootScope.$broadcast('saveCallback',data);
});
Run Code Online (Sandbox Code Playgroud)小智 6
我创建了一个控制路径路径模式之间共享范围的工厂,因此您可以在用户在同一路径父路径中导航时维护共享数据.
.controller('CadastroController', ['$scope', 'RouteSharedScope',
function($scope, routeSharedScope) {
var customerScope = routeSharedScope.scopeFor('/Customer');
//var indexScope = routeSharedScope.scopeFor('/');
}
])
Run Code Online (Sandbox Code Playgroud)
因此,如果用户转到另一个路径路径,例如'/ Support',路径'/ Customer'的共享数据将自动销毁.但是,如果不是这样,用户转到'子'路径,如'/ Customer/1'或'/ Customer/list',则不会破坏范围.
您可以在此处查看示例:http://plnkr.co/edit/OL8of9
有多种方法可以在控制器之间共享数据
众所周知$rootscope,数据传输或通信不是首选方法,因为它是一个全局范围,可用于整个应用程序
对于Angular Js控制器之间的数据共享,Angular服务是最佳实践,例如。.factory,.service
供参考
在从父数据传输到子控制器的情况下,你可以通过孩子控制器直接访问父数据$scope
如果您使用的ui-router话,可以用$stateParmas传递URL参数,如id,name,key,等
$broadcast这也是在控制器之间从父级到子级之间$emit传输数据以及从子级到父级控制器之间传输数据的好方法
的HTML
<div ng-controller="FirstCtrl">
<input type="text" ng-model="FirstName">
<br>Input is : <strong>{{FirstName}}</strong>
</div>
<hr>
<div ng-controller="SecondCtrl">
Input should also be here: {{FirstName}}
</div>
Run Code Online (Sandbox Code Playgroud)
JS
myApp.controller('FirstCtrl', function( $rootScope, Data ){
$rootScope.$broadcast('myData', {'FirstName': 'Peter'})
});
myApp.controller('SecondCtrl', function( $rootScope, Data ){
$rootScope.$on('myData', function(event, data) {
$scope.FirstName = data;
console.log(data); // Check in console how data is coming
});
});
Run Code Online (Sandbox Code Playgroud)
请参阅给定的链接以了解更多信息$broadcast
我使用了AngularJS 服务。
第 1步:我创建了一个名为 SharedDataService 的 AngularJS 服务。
myApp.service('SharedDataService', function () {
var Person = {
name: ''
};
return Person;
});
Run Code Online (Sandbox Code Playgroud)
Step2:创建两个控制器,使用上面创建的服务。
//First Controller
myApp.controller("FirstCtrl", ['$scope', 'SharedDataService',
function ($scope, SharedDataService) {
$scope.Person = SharedDataService;
}]);
//Second Controller
myApp.controller("SecondCtrl", ['$scope', 'SharedDataService',
function ($scope, SharedDataService) {
$scope.Person = SharedDataService;
}]);
Run Code Online (Sandbox Code Playgroud)
Step3:只需在视图中使用创建的控制器。
<body ng-app="myApp">
<div ng-controller="FirstCtrl">
<input type="text" ng-model="Person.name">
<br>Input is : <strong>{{Person.name}}</strong>
</div>
<hr>
<div ng-controller="SecondCtrl">
Input should also be here: {{Person.name}}
</div>
</body>
Run Code Online (Sandbox Code Playgroud)
要查看此问题的有效解决方案,请按以下链接
https://codepen.io/wins/pen/bmoYLr
.html 文件:
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body ng-app="myApp">
<div ng-controller="FirstCtrl">
<input type="text" ng-model="Person.name">
<br>Input is : <strong>{{Person.name}}</strong>
</div>
<hr>
<div ng-controller="SecondCtrl">
Input should also be here: {{Person.name}}
</div>
//Script starts from here
<script>
var myApp = angular.module("myApp",[]);
//create SharedDataService
myApp.service('SharedDataService', function () {
var Person = {
name: ''
};
return Person;
});
//First Controller
myApp.controller("FirstCtrl", ['$scope', 'SharedDataService',
function ($scope, SharedDataService) {
$scope.Person = SharedDataService;
}]);
//Second Controller
myApp.controller("SecondCtrl", ['$scope', 'SharedDataService',
function ($scope, SharedDataService) {
$scope.Person = SharedDataService;
}]);
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
262969 次 |
| 最近记录: |