這是關於Angular JS One的,所以我在我的plunkr中創建了一個簡單的應用程序。 我做了什麼? ,我創建了一個名爲app.js和myController.js的工廠。AngularJS 1 - 如何將app.js值列表傳遞給controller.js
問題是來自controller.js的Get方法,我不知道如何正確編寫它,因此它會將數據列表傳遞給html。請檢查方法getData(app.js)和Get(controller.js)。有什麼錯誤?
注意:添加和編輯工作正常使用這三個層(app-> controller-> view),我需要。
PS:我什麼都沒做。
謝謝! 約翰
下面是我的app.js或業務
angular.module('myFirstApp',['ngRoute'])
.config(function($routeProvider){
$routeProvider
.when('/home',{templateUrl : 'home.html'})
.when('/people',{templateUrl : 'people.html'})
.when('/about',{templateUrl : 'about.html'})
.when('/contact',{templateUrl : 'contact.html'})
})
.factory('personFactory',function(){
function getData(){
$http.get('iphone.json').success(function(result){
return result;
}).error(function(error){
alert(error.error + "/" + error.statusCode);
});
}
function insertData(Name,Email,Password,Mobile){
//update data to database
var x = "Record added successfuly.";
return x;
}
function updateData(){
//update data to database
var x = "Record updated successfuly.";
return x;
}
return {
getData : getData,
insertData,
updateData
};
})
下面是我controller.js
angular.module('myFirstApp')
.controller('myController',function($scope,$http,personFactory){
$scope.Add = function(){
var x = personFactory.insertData($scope.Name,$scope.Email,$scope.Password,$scope.Mobile,$scope.result);
$scope.Message = x;
return $scope.Message;
}
$scope.Edit = function(){
var x = personFactory.updateData();
$scope.Message = x;
return $scope.Message;
}
$scope.Get = function(){
var x = personFactory.getData();
$scope.PeopleList = x;
return $scope.PeopleList;
}
})
下面是我的看法
<html>
<head>
<title></title>
<script src="app.js"></script>
</head>
<body>
<p>People</p>
<table ng-controller='myController'>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Model</th>
<th>Status</th>
<th>Purchased on</th>
</tr>
</thead>
<tr ng-repeat="values in PeopleList">
<td>{{values.name}}</td>
<td>{{values.email}}</td>
<td>{{values.model}}</td>
<td>{{values.status}}</td>
<td>{{values.purchasedate}}</td>
</tr>
</table>
</body>
</html>
接下來會發生什麼? –