我主要遵循 Ryan Bates 为 rails 中的 angular 应用程序设置。在我的宝石文件中:
gem 'angularjs-rails'
在 application.js 中:
//= require angular
//= require angular-resource
//= require turbolinks
//= require_tree .
这是我认为来自views/pages.home.html的所有相关代码:
<body data-ng-app="dithat">
<div class="container" data-ng-controller="accomplishmentController">
<p> What'd you do? </p>
<form ng-submit="submit()">
<input type="text" ng-model="newAccomp" />
</form>
<div data-ng-repeat="accomp in accomplishments | filter:newAccomp" >
<div class="box" ng-click="addToCount()">
<div class="accomplishment">
{{ accomp.name }}
<a href="#" ng-click="delete()" class="x">x</a>
<p class="count"> {{ accomp.count }} </p>
</div>
</div>
</div>
</div>
<script type="text/javascript">
app = angular.module("dithat", ["ngResource"]);
function accomplishmentController($scope, $resource) {
Entry = $resource('/api/users.json');
console.log(Entry.query());
$scope.accomplishments = [];
$scope.submit = function() {
$scope.accomplishments.unshift({ name: $scope.newAccomp, count: 0 });
$scope.newAccomp = '';
}
$scope.addToCount = function() {
var currentcount = this.accomp.count;
this.accomp.count = currentcount + 1;
}
$scope.delete = function() {
index = this.$index;
$scope.accomplishments.splice(index, 1)
}
}
</script>
</body>
该代码可以正常工作,因为应用程序的行为方式应如此,但它没有进行资源调用。我也试过这个,$http
但也没有用。我错过了什么??!!非常感谢!