Javascript 如何在angular js中从控制器加载部分数据
我想通过http.post方法访问部分文件中的数据。 当我将响应放在console.logJSON.stringifyres中时,数据可以在controller中访问,但无法在partila file.html中访问。res显示在浏览器控制台中。这意味着响应来自controller。 但是chill.html中未显示部分响应。如何访问chill.html中的响应。 我还使用了post方法,因为我正在将一些数据发布到/a 这是控制器的代码Javascript 如何在angular js中从控制器加载部分数据,javascript,angularjs,node.js,html,express,Javascript,Angularjs,Node.js,Html,Express,我想通过http.post方法访问部分文件中的数据。 当我将响应放在console.logJSON.stringifyres中时,数据可以在controller中访问,但无法在partila file.html中访问。res显示在浏览器控制台中。这意味着响应来自controller。 但是chill.html中未显示部分响应。如何访问chill.html中的响应。 我还使用了post方法,因为我正在将一些数据发布到/a 这是控制器的代码 var myApp = angular.module('m
var myApp = angular.module('myApp', ['ui.router']);
myApp.config(function($stateProvider, $urlRouterProvider,$locationProvider) {
$stateProvider
// HOME STATES AND NESTED VIEWS ========================================
.state('home',{
url: '/',
templateUrl: 'partials/final1.html'
})
.state('a',{
url:'/a',
templateUrl:'partials/chill.html',
controller: 'searchctrl'
}
})
myApp.controller('searchctrl',function($scope,$http,$location){
// app.searchctrl($scope,$http,$location)
$scope.search=function(){
$http.post('/a',$scope.user).success(function(res){
$scope.items=res;
console.log(JSON.stringify(res));
$location.path('/a');
}).
error(function(res){
alert(1234);
})
}
});
chill.html
<div class="right-content">
<div class="right-content-heading">
<div class="right-content-heading-left">
<h3>Latest Colletcions of videos</h3>
</div>
<div class="right-content-heading-right">
<div class="social-icons">
<ul>
<li><a href="#" target="_blank" class="facebook"> </a></li>
<li><a href="#" target="_blank" class="twitter"></a></li>
<li><a href="#" target="_blank" class="googleplus"></a></li>
<li><a href="#" target="_blank" class="pinterest"></a></li>
<li><a href="#" target="_blank" class="dribbble"></a></li>
<li><a href="#" target="_blank" class="vimeo"></a></li>
</ul>
</div>
</div>
<div class="clear"> </div>
</div>
<!-- -pictures--->
<ul>
<li ng-repeat="man in items">{{ man.name }}</li>
</ul>
<div class="clear"></div>
<!-- //////////////////////////////////-->
</div>
那么,如何在controller searachctrl中加载chill.html文件中的数据。data在搜索函数中设置为$scope.items,何时/如何调用此函数??此函数通过提交表单调用。这个很好用。我使用http.post向app.post路由器提交一些数据,并使用服务器中的req.body获取这些数据。然后从app.post使用res.jsonims将服务器数据发送回客户端,并使用http.post方法在相同的函数/控制器searchctrl中获取数据。数据来自res变量中的服务器。我还有console.logres来检查数据是否来了。所有这些都很好,res中有数据。
app.post('/a',function(req,res){
var string=req.body.name;
//console.log(string);
items = [
{
name: 'Macallan 12',
price: 50
},
{
name: 'Chivas Regal Royal Salute',
price: 10000
},
{
name: 'Glenfiddich 1937',
price: 20000
}
];
res.json(items);
});