[英]How to call/use 1 controller of $scope in another controller in AngularJS
My question is that how to call 1 controller $scope to another controller in Angular JS. 我的问题是如何在Angular JS中调用1个控制器$ scope到另一个控制器。 Bascially The structure is that, I have two controller one is AddController and the other one is ViewController.
基本上,结构是,我有两个控制器,一个是AddController,另一个是ViewController。 Basically I want the form with fields of AddController in ViewController.
基本上我想要在ViewController中具有AddController字段的表单。 $rootScope.name = data.name, email and phone is not showing the data on the form fields once I click on Edit.
$ rootScope.name = data.name,单击“编辑”后,电子邮件和电话未在表单字段上显示数据。
What I've already tried. 我已经尝试过的。
In HTML 在HTML中
<div ng-app="app">
<div ng-controller="AddController">
<legend>Add Students</legend>
<form class="well">
<label>Name</label>
<input type="text" name="name" ng-model="name" />
<label>Email</label>
<input type="text" name="email" ng-model="email" />
<label>Phone</label>
<input type="text" name="phone" ng-model="phone" />
<br/>
<div ng-if="isUpdate">
<input type="hidden" name="id" ng-model="id" />
</div>
<div ng-show="addData">
<input type="button" value="Save" ng-click="saveStudent()" class="btn btn-primary" />
</div>
<div ng-show="!addData">
<input type="button" value="Update" ng-click="updateStudent()" class="btn btn-primary" />
</div>
</form>
</div>
<div ng-controller="ViewController">
<legend>View Students</legend>
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Phone</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="student in students">
<td>{{ $index; }}</td>
<td>{{ student.name }}</td>
<td>{{ student.email }}</td>
<td>{{ student.phone }}</td>
<td><a href="javascript:void(0)" ng-click="edit($index)">Edit</a> | <a href="javascript:void(0)" ng-click="delete($index)">Delete</a></td>
</tr>
</tbody>
</table>
</div>
</div>
In JS 在JS中
var app = angular.module('app', []);
app.service('StudentsData', function($rootScope){
var s;
var students = [
{name: 'Tariq Ali', email: 'chk.webstar@gmail.com', phone: '58757'},
{name: 'Faizan Ali', email: 'kdjy.webstar@gmail.com', phone: '24545'}
];
this.list = function(){
return students;
}
this.single = function(id){
for(s=0; s<=students.length; s++){
if(s == id){
return students[s];
}
}
}
this.insert = function(data){
students.push(data);
}
this.update = function(updatedData, id){
for(s=0; s<=students.length; s++){
if(s == id){
students[s] = updatedData;
}
}
}
this.delete = function(id){
students.splice(id, 1);
}
this.name = function(){
}
});
app.controller('AddController', function($scope, StudentsData, $rootScope){
$scope.$broadcast('someEvent', [1,2,3]);
$rootScope.addData = true;
$scope.saveStudent = function(){
StudentsData.insert({name: $scope.name, email: $scope.email, phone: $scope.phone});
$scope.name = '';
$scope.email = '';
$scope.phone = '';
}
$scope.updateStudent = function(){
var updatedData = {name: $scope.name, email: $scope.email, phone: $scope.phone};
StudentsData.update(updatedData, $scope.id);
/*$scope.name = '';
$scope.email = '';
$scope.phone = '';
$rootScope.addData = true;*/
}
});
app.controller('ViewController', function($scope, StudentsData, $rootScope){
$scope.$on('someEvent', function(event, mass) {console.log(mass)});
$scope.students = StudentsData.list();
$scope.delete = function(id){
StudentsData.delete(id);
}
$scope.edit = function(id){
var data = StudentsData.single(id);
$rootScope.name = data.name;
$rootScope.email = data.email;
$rootScope.phone = data.phone;
$rootScope.addData = false;
$rootScope.isUpdate = true;
$rootScope.id = id;
//console.log($rootScope.addData);
//StudentsData.update(id);
}
});
Here is my fiddler http://jsfiddle.net/npm5u5h0/ For first time if you click on edit its working fine but if you add a new student and after edit the newly created student the textfields are not showing the data. 这是我的提琴手http://jsfiddle.net/npm5u5h0/第一次,如果单击,可以正常工作,但是如果您添加了一个新学生,并且在编辑了新创建的学生之后,文本字段将不会显示数据。
Why not have two controllers that both call the same service? 为什么没有两个都调用相同服务的控制器? They you could put the information in the service and have them both reference that.
他们可以将信息放入服务中,并让它们都引用该信息。
angular.module('mycontrollers').controller('controllername', ['myService', function (myService) {
$scope.email = myService.getEmail;
}]);
angular.module('mycontrollers').controller('controllername2', ['myService', function (myService) {
$scope.email = myService.getEmail;
}]);
angular.module('myservices').service('myService', ['$rootScope', function ($rootScope) {
var self = this;
self.getEmail = function () {
...
return $rootScope.email;
};
self.setEmail = function (email) {
$rootScope.email = email;
};
}]);
You could name the addContoller div with an ID such as "addController" 您可以使用诸如“ addController”之类的ID来命名addContoller div
Then from within your viewController, you could grab the other scope with 然后从您的viewController中,您可以使用
var myEl = angular.element(document.querySelector('#addController')); var myEl = angular.element(document.querySelector('#addController')); var myScope = angular.element(myEl).scope();
var myScope = angular.element(myEl).scope();
and then $scope.name = myScope.name 然后$ scope.name = myScope.name
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.