2016-12-03 99 views
0

好了,用下面的代码 - 我只得到{{味精}}的“details.html”页面上的回报。我可以得到文字等.....但没有别的。我相信这是简单的,但我想要做的是,使得当我点击按钮时,“todo”的标题显示在我的列表中。任何帮助将不胜感激。点击'细节'按钮应该使标题出现在实际列表下。困惑的角路由

 <!DOCTYPE html> 
<html ng-app="homework"> 
    <head> 
    <meta charset="utf-8"> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <title>Untitled Document</title> 
    <!-- Bootstrap --> 
    <link href="css/bootstrap.css" rel="stylesheet"> 
     <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.9/angular.min.js"></script> 
     <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-route.js"></script> 
    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries --> 
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// --> 
    <!--[if lt IE 9]> 
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script> 
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script> 
     <![endif]--> 
    </head> 
    <body> 

    <html ng-app="homework"> 
    <body> 

        <form ng-controller="TodoController as todoCtrl" 
          ng-submit="addTodo()"> 
          <!-- add Bootstrap and AngularJS HTML Form here --> 
          <div class="form-group col-sm-6"> 
           <label for="todoTitle">Short Name</label> 
           <input id="todoTitle" class="form-control" type="text" 
            ng-model="todoTitle"> 
          </div> 
          <div class="form-group col-sm-6"> 
           <label for="todoDescription">Description</label> 
           <input id="todoDescription" class="form-control" type="text" 
            ng-model="todoDescription"> 
          </div> 
          <div class="form-group"> 
           <input class="btn btn-primary" type="submit" value="Add New"> 
          </div> 
          <!-- Display table if todoList has more than one object --> 
          <div ng-if="todoList.length > 0"> 
           <table class="table table-striped"> 
            <th>ToDo Name</th> 
            <th>Description</th> 
            <th>Date</th> 
            <th>Complete</th> 
            <!-- loop over arry of todos --> 
            <tr ng-repeat="todo in todoList track by $index"> 
             <td ng-bind="todo.title"></td> 
             <td ng-bind="todo.description"></td> 
             <td ng-bind="todo.date | date:'MMM d, yyyy'"></td> 
             <td ng-click="deleteTodo()"><span class="glyphicon glyphicon-remove" style="color: red;"></span></td> 
             <td><a href="#details">Details</a></td> 
             <td ng-click="testTodo()"><span class="glyphicon glyphicon-ok" style="color: blue;"></span></td> 
            </tr> 
           </table> 
          </div> 
        </form>  

     <div ng-view></div> 




    <script> 
    var app = angular.module('homework', ['ngRoute']); 
         app.controller('TodoController', function($scope) { 
          $scope.todoList = []; 

          $scope.addTodo = function() { 
           $scope.todoList.push({ 
            title:$scope.todoTitle, 
            description:$scope.todoDescription, 
            date:new Date() 
            }); 
           $scope.todoTitle = ""; 
           $scope.todoDescription = ""; 
          }; 

          $scope.deleteTodo = function(){ 
           $scope.todoList.splice(this.$index, 1); 
          }; 

          $scope.testTodo = function(){ 
           alert(this.todo.title); 
          } 
         }); 

     app.config(function($routeProvider){ 
      $routeProvider 
      .when("/", { 
       templateUrl: "blank.html" 
      }) 
      .when("/details", { 
       templateUrl: "details.html", 
       controller: "detailsCtrl" 
      }); 
    }) 
     app.controller("detailsCtrl", function($scope){ 
      $scope.msg = (todo.title); 
     }); 


     </script> 

     </body> 
</html> 






    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> 
    <script src="js/jquery-1.11.3.min.js"></script> 

    <!-- Include all compiled plugins (below), or include individual files as needed --> 
    <script src="js/bootstrap.js"></script> 
    </body> 
</html> 

“details.html”

<!doctype html> 
<html> 
    <h1>{{msg}}</h1> 
</html> 
+0

Wazzup与底部代码这个工作,你有'后的jQuery'? – Endless

回答

0

的问题是,在

app.controller("detailsCtrl", function($scope) { 
    $scope.msg = (todo.title); 
}); 

这里待办事项是不确定的,它会创建一个新的实例,因此待办事项变得不确定。 您需要使用服务/工厂在控制器之间传递数据。 你可以看到通过改变

app.controller("detailsCtrl", function($scope) { 
    $scope.msg = "test"; 
}); 

DEMO

+0

我已经可以使用字符串来通过。这不是我问的问题。 –