2017-03-17 205 views
0

我正在排除一個能夠打開index.htmltitleDetails.html的特定標題的網頁。AngularJS ng-click突然停止工作

但是,ng-click在我的index.html突然停止工作。我沒有做任何可能影響鏈接的改變。它一直在正常工作(從index.html到titleDetails.html的頁面重定向)。

Original post here

下面是我的代碼:

app.js

(function() { 
    angular 
    .module("BlogApp", []) 
    .controller("BlogController", BlogController); 

    function BlogController($scope, $http) { 
     $scope.createPost = createPost; 
     $scope.deletePost = deletePost; 
     $scope.editPost = editPost; 
     $scope.updatePost = updatePost; 
     $scope.postDetail = null; 

    function init() { 
     getAllPosts(); 
    } 
    init(); 

    function titleDetails(post){ 
     $scope.postDetail = post; 
     window.location = "/titleDetails.html"; 
    } 

    function updatePost(post){ 
     console.log(post); 
     $http 
     .put("/api/blogpost/"+post._id, post) 
     .success(getAllPosts); 
    } 

    function editPost(postId){ 
     $http 
     .get("/api/blogpost/"+postId) 
     .success(function(post){ 
      $scope.post = post; 
     }); 
    } 

     function deletePost(postId){ 
      $http 
      .delete("/api/blogpost/"+postId) 
      .success(getAllPosts); 
     } 

     function getAllPosts(){ 
      $http 
      .get("/api/blogpost") 
      .success(function(posts) { 
       $scope.posts = posts; 
      }); 
     } 

     function createPost(post) { 
      console.log(post); 
      $http 
      .post("/api/blogpost",post) 
      .success(getAllPosts); 
     } 
    } 
})(); 

的index.html

<!DOCTYPE html> 
<html lang="en" ng-app="BlogApp"> 
<head> 
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script> 
    <script src="app.js"></script> 
    <title>Title</title> 
</head> 
<body> 
    <div class="container" ng-controller="BlogController"> 
     <h1>Blog</h1> 
      <input ng-model="post.title" class="form-control" placeholder="title"/> 
      <textarea ng-model="post.body" class="form-control" placeholder="body"></textarea> 
      <button ng-click="createPost(post)" class="btn btn-primary btn-block">Post</button> 
      <button ng-click="updatePost(post)" class="btn btn-success btn-block">Update</button> 

      <div ng-repeat="post in posts"> 
       <h2> 
       <a ng-click="titleDetails(post)">{{ post.title }} </a> 
        <a ng-click="editPost(post._id)" class="pull-right"><span class="glyphicon glyphicon-pencil"></span></a> 
        <a ng-click="deletePost(post._id)" class="pull-right"><span class = "glyphicon glyphicon-remove"></span></a> 
       </h2> 
       <em>{{post.posted}}</em> 
       <p>{{post.body}}</p> 
      </div> 
    </div> 
</body> 
</html> 

titleDetails.html:

<!DOCTYPE html> 
<html lang="en" ng-app="BlogApp"> 
<head> 
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script> 
    <script src="app.js"></script> 
    <title>Title</title> 
</head> 
<body> 
    <div class="container" ng-controller="BlogController"> 
     <h1>Blog</h1> 
      <div> 
       <h2> 
        <a>{{ postDetail.title }} </a> 
       </h2> 

       <em>{{postDetail.posted}}</em> 
       <p>{{postDetail.body}}</p> 
      </div> 
    </div> 
</body> 
</html> 
+0

從它的外觀你缺少控制器中的'$ scope.titleDetails = titleDetails'。 – eminlala

+0

謝謝@ex0dm3nt! – gymcode

+0

我的榮幸:)。我會以附加建議的形式發佈這個答案。 – eminlala

回答

1

你缺少你的控制器$scope.titleDetails = titleDetails;

此外,我會建議使用controller as語法。

因此,這將是這樣的:

的index.html

<div class="container" ng-controller="BlogController as blogCtrl"> 
... 
<a ng-click="blogCtrl.titleDetails(post)">{{ blogCtrl.post.title }} </a> 

控制器

function BlogController($scope, $http) { 
    var vm = this; 
    vm.titleDetails = titleDetails; 
    //rest of your code using 'vm' instead of '$scope' 

這樣,您就可以停止使用$scope

您可以找到更多詳細信息here