2016-06-20 63 views
2

我一直在角度控制器中遇到這個問題。我儘可能地查找了它,但我無法解決問題。角度雙向約束

我想實現一個簡單的控制器,但對於我的生活,我無法獲得綁定工作。它不顯示我的數據。例如,當我說{{ test }}時,我明白了,而不是「Hello World!」。串。

var app = angular.module('App', []); 
 

 
app.controller('Hi', function($scope){ 
 
\t $scope.hello = "hello!"; 
 
}); 
 

 
app.controller('todoCtrl', ['$scope', '$http', function($scope, $http) { 
 
\t $scope.test = "Hello World!"; 
 
\t $scope.formData = ""; 
 
\t 
 
\t $http.get('/api/todos') 
 
\t \t .success(function(data) { 
 
\t \t \t $scope.todos = data; 
 
\t \t \t console.log(data); 
 
\t \t }) 
 
\t \t .error(function(data) { 
 
\t \t \t console.log('Error: ' + data); 
 
\t \t }); 
 
\t 
 
\t $scope.createTodo = function() { 
 
\t \t $http.post('/api/todos', $scope.formData) 
 
\t \t \t .success(function(data) { 
 
\t \t \t \t $scope.formData.text = ""; 
 
\t \t \t \t $scope.todos = data; 
 
\t \t \t \t console.log(data); 
 
\t \t }) 
 
\t \t \t .error(function(data) { 
 
\t \t \t \t console.log('Error: ' + data); 
 
\t \t }); 
 
\t }; 
 
\t 
 
\t $scope.deleteTodo = function(id) { 
 
\t \t $http.delete('/api/todos/' + id) 
 
\t \t \t .success(function(data) { 
 
\t \t \t \t $scope.todos = data; 
 
\t \t \t \t console.log(data); 
 
\t \t }) 
 
\t \t \t .error(function(data) { 
 
\t \t \t \t console.log('Error: ' + data); 
 
\t \t }); 
 
\t }; 
 
}]);
<!DOCTYPE html> 
 
<html ng-app="App"> 
 
\t <head> 
 
\t \t <title>TodoX</title> 
 
\t \t <meta name="viewport" content="width=device-width, initial-scale=1"> 
 
\t \t 
 
\t \t <!-- Bootstrap CSS --> 
 
\t \t <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> 
 
\t \t <!-- TodoX CSS --> 
 
\t \t <link rel="stylesheet" type="text/css" href="stylesheets/style.css"/> 
 
\t </head> 
 
\t <body ng-controller="todoCtrl"> 
 
\t \t <div class="container"> 
 
\t \t \t <div class="row"> 
 
\t \t \t \t <div class="jumbotron text-center"> 
 
\t \t \t \t \t <h1>TodoX<span>{{todos.length}}</span>{{test}}</h1> 
 
\t \t \t \t </div> 
 
\t \t \t \t <div class="col-md-8"> 
 
\t \t \t \t \t <div class="list-group"> 
 
\t \t \t \t \t \t <div class="checkbox" ng-repeat="todo in todos | orderBy:date"> 
 
\t \t \t \t \t \t \t <label class="list-group-item"> 
 
\t \t \t \t \t \t \t \t <input type="checkbox"/> {{todo.text}} 
 
\t \t \t \t \t \t \t </label> 
 
\t \t \t \t \t \t </div> 
 
\t \t \t \t \t </div> 
 
\t \t \t \t </div> 
 
\t \t \t \t <div class="col-md-4"> 
 
\t \t \t \t \t <form class="form-group"> 
 
\t \t \t \t \t \t <input type="text" class="form-control" ng-model="formData"/> 
 
\t \t \t \t \t \t <input type="submit" ng-click="createTodo()" placeholder="Submit" class="form-control"/> 
 
\t \t \t \t \t </form> 
 
\t \t \t \t </div> 
 
\t \t \t </div> 
 
\t \t </div> 
 
\t \t 
 
\t \t <!-- Angular JS --> 
 
\t \t <script type="text/javascript" href="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script> 
 
\t \t <!-- TodoX Core JS --> 
 
\t \t <script type="text/javascript" href="core.js"></script> 
 
\t </body> 
 
</html>

+0

什麼是不工作? – ajmajmajma

+0

它沒有顯示我的數據。例如,當我說{{test}}時,我明白了,而不是Hello World!串。 – Ajlanclos

+0

而且,它在Angular的適當文件路徑中正確鏈接。我甚至嘗試過CDN。 – Ajlanclos

回答

3

我只是執行你的代碼,同時將腳本標籤上面的角文件鏈接,使AngularJs裝載前腳本可以調用角模塊。

我認爲你在腳本之後放置角,這就是爲什麼你會遇到這個問題。你的代碼工作得很好。我測試了它。

把它像這樣

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script> 
<script src="script.js"></script> 

這裏script.js將控制器腳本。

Working fiddle

+0

哇,我只是想出了我的錯誤,我使用HREF而不是SRC來鏈接Javascript文件,這是一個愚蠢的錯誤。 – Ajlanclos