有NG专家吗?
我无法让我的工厂 (VIEWservices) 将 JSON 添加到新视图 casetudy.php。
我想要实现的是,当您单击具有功能(openCase)的帖子时,它会在 ng-view 中显示 casetudy.php(有效),但我无法让它显示该帖子的相关内容。
我认为这是一个非常简单的解决方法,只是看了几个小时并且没有任何乐趣。
HTML
<div ng-controller="homeCtrl">
<div ng-repeat="post in posts">
<h1 ng-bind="post.title"></h1>
<a ng-click="openCase(post.slug)">view more</a>
<p ng-bind="post.id"></p>
</div>
<!-- page content -->
<div ng-view></div>
</div>
JS
var app = angular.module('appname', ['ngSanitize'])
app.config(function ($routeProvider, $locationProvider){
$locationProvider.hashPrefix("!")
$routeProvider
.when('/:slug',
{
templateUrl: 'wp-content/themes/name/views/casestudy.php',
controller: 'caseCtrl'
})
.otherwise('/')
});
app.controller('homeCtrl', function($scope, VIEWservices, $route, $location) {
$scope.posts = VIEWservices.getPosts();
console.log($scope.posts);
$scope.openCase = function(caseSLUG) {
// open a post by its slug
console.log(caseSLUG);
$location.path("/" + caseSLUG)
}
});
app.controller('caseCtrl', function($scope, VIEWservices, $routeParams) {
$scope.post = VIEWservices.getPosts()[$routeParams.slug];
console.log($scope.post);
});
app.factory('VIEWservices', function($http) {
return {
getPosts: function() {
return $http.get('wp-content/themes/name/ajax/homepage-casestudies.php').then(function (result) {
return result.data;
})
}
}
});