问题描述
我是angularjs的新手.我想将数据从html表单传递到另一条路由.
I am new to angularjs. I want to pass data from html form to another route.
这是 index.html
<div ng-app="myApp" ng-controller="HomeController">
<div class="container">
<div class="row">
<div class="col-md-12">
<div ng-view=""></div>
</div>
</div>
</div>
</div>
这是路线
var myApp = angular.module('myApp', ['ngRoute']);
myApp.config(['$routeProvider', function ($routeProvider) {
$routeProvider.when('/', {
templateUrl: 'views/home.html',
controller: 'HomeController'
});
$routeProvider.when('/about', {
templateUrl: 'views/about.html',
controller: 'AboutController'
});
}]);
当路线为/时,它会命中具有表单的 views/home.html
When the route is / it hits the views/home.html in which it has a form
<form action="#/about" ng-submit="submitData()">
<input type="text" name="address" ng-model="user.address" />
<input type="submit" />
</form>
我有一个用户服务,其实现方式为
I have a user service whose implementation is
myApp.factory("user", function () {
return {};
});
我像
myApp.controller("HomeController", function ($scope, user) {
$scope.user = user;
// and then set values on the object
// $scope.user.address = "1, Mars"; // when uncomment this line it can be accessed on AboutController? Why? Otherwise I cannot access user.address
console.log($scope.user);
});
请不要在上面的代码中注释我的评论.
Don note my comment in above code..
并将用户传递给AboutController之类的
and passes user to AboutController like
myApp.controller("AboutController", function ($scope, user) {
$scope.user = user;
// and then set values on the object
$scope.user.firstname = "John";
$scope.user.secondname = "Smith";
console.log($scope.user);
});
这是 about.html
<p>
{{ user.address }}
</p>
问题:{{user.address}}在 AboutController 上不起作用.我看不到任何输出...但是当我从上述代码中删除注释时..它仅在控制器中显示硬编码的值.我想念什么?
Problem: {{user.address}} doesn't work on AboutController. I can't see any output... But when i remove the comment from above code.. It only displays hardcoded values in the controller What am I missing?
这是工作示例 http://ashoo.com.au/angular/#/
推荐答案
此刻,您所做的所有服务就是将空白对象 return {}
传递给注入它的任何控制器.您需要一种getter/setter方法,以便在 Home
视图中可以设置数据,而在 About
视图中可以检索数据.
At the moment, all your service does is pass a blank object return {}
, to any controller into which it is injected. You need a getter/setter approach, so that in your Home
view you can set the data, and in your About
view you can retrieve it.
因此您的服务可能如下所示:
So your service could look something like this:
myApp.factory("user", function () {
var dataObj = {};
return {
setData: function(data) {
dataObj.username = data.username;
},
getData: function() {
return dataObj;
}
};
});
现在,您可以在 Home
控制器中设置数据了:
Now you can set the data in your Home
controller:
myApp.controller("HomeController", function ($scope, user) {
$scope.submitData = function(data) { //pass in ng-model
user.setData(data); //call 'user' service to set data
}
});
并从您的 About
控制器中调用它:
And call it from your About
controller:
myApp.controller("AboutController", function ($scope, user) {
$scope.user = user.getData(); //Assign
console.log($scope.user.username);
});
您的html看起来像这样:
And you html would look like:
<form action="#/about" ng-submit="submitData(user.username)">
<input type="text" name="address" ng-model="user.username" />
<input type="submit" />
</form>
这篇关于将数据从角度形式传递到另一个控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!