就像我想確保某人註冊時沒有使用預先存在的電子郵件地址一樣。如何使AngularJS文件與Rails用戶模型/用戶數據庫交互以查看? NodeJS在這裏是必需的嗎?這可以通過一個「獨特的」指令來完成嗎(我希望遵循的方法)?如何讓AngularJS檢查和使用Rails後端的數據?
我正在使用AngularJS的1.3.0-beta.18版本。我不知道這是如何改變的 - 是否仍然使用$ parse函數?如何使用$ validators管道設置一個setValidity?
編輯: 我遇到這個例子從http://www.ng-newsletter.com/posts/validations.html。我如何使它適應典型的Rails設置 - 用戶控制器,用戶模型和用戶數據庫?如果我在用戶控制器中有一個「唯一」方法來查詢用戶模型,以查看是否有包含該電子郵件參數的條目,該怎麼辦?這個「唯一」方法由「唯一」的AngularJS指令調用,並且在註冊表單中調用該指令。當我做「耙路」時,我已經有了一條「獨特的」JSON路線,以及一條「獨特的」HTML路線。我知道如何在表單中調用指令以及如何在表單中進行錯誤檢查,所以這不是問題。
app.directive('ensureUnique', ['$http', function($http) {
return {
require: 'ngModel',
link: function(scope, ele, attrs, c) {
scope.$watch(attrs.ngModel, function() {
$http({
method: 'POST',
url: '/api/check/' + attrs.ensureUnique,
data: {'field': attrs.ensureUnique}
}).success(function(data, status, headers, cfg) {
c.$setValidity('unique', data.isUnique);
}).error(function(data, status, headers, cfg) {
c.$setValidity('unique', false);
});
});
}
}
}]);
最新編輯2014年8月29日: 所以這是我與工作的代碼,但它不工作。表單提交對於FormCtrl控制器和用戶工廠來說非常好,但UniqueEmail工廠和EnsureUnique指令不起作用。所有標準的AngularJS指令(如ng-minlength)在HTML表單中工作正常。
我有Rails的路線api/users/unique和users/unique。我不太確定使用哪個,我假設api/users/unique?
這是我的Rails控制器方法「獨特的」的代碼:
class UsersController < ApplicationController
include AngularController
def new
@user = User.new
respond_with @employee
end
def create
@user = User.create(params.permit(:email, :name, :password, :password_confirmation))
if @user.save
session[:@user_id] = @user.id
redirect_to root_url
else
render 'new'
end
end
def unique
if User.exists?(:email => params[:email])
return true
end
end
end
module AngularController
extend ActiveSupport::Concern
included do
respond_to :html, :json
around_action :without_root_in_json
end
def without_root_in_json
ActiveRecord::Base.include_root_in_json = false
yield
ActiveRecord::Base.include_root_in_json = true
end
end
這是我的AngularJS代碼:
mainapp.factory("UniqueEmail", function($resource) {
return {
states: $resource('/users/unique.json', {}, {
query: { method: 'GET', params: {}, isArray: false }
})
};
});
mainapp.factory("User", function($resource) {
return $resource("https://stackoverflow.com/users/:id", {
id: "@id"
});
});
app.directive('ensureUnique', ['$http', function($http) {
return {
require: 'ngModel',
link: function(scope, ele, attrs, c) {
scope.$watch(attrs.ngModel, function(value) {
$http.get({
url: '/users/unique',
params: {"email": value}
}).success(function(data, status, headers, cfg) {
c.$setValidity('unique', false);
}).error(function(data, status, headers, cfg) {
c.$setValidity('unique', true);
});
});
}
}
}]);
mainapp.controller("FormCtrl", function($scope, User, UniqueEmail) {
$scope.user = new User();
$scope.users = User.query();
$scope.password_confirmation = null;
$scope.add = function() {
$scope.users.push(User.save({
name: $scope.user.name,
email: $scope.user.email,
password: $scope.user.password,
password_confirmation: $scope.user.password_confirmation
}));
return $scope.user = new User();
};
return $scope["delete"] = function($index) {
if (confirm("Are you sure you want to delete this user?")) {
$scope.users[$index].$delete();
return $scope.users.splice($index, 1);
}
};
});
你能給一個AngularJS指令的例子,用Rails控制器方法來查詢Rails後端來訪問數據並將其返回給指令以使其有效或無效嗎?我看到以前版本的AngularJS的一個例子,但它不起作用 - 我想因爲AngularJS的重大變化,儘管我不確定。這裏是我發現的資源:http://blog.berylliumwork.com/2012/10/tutorials-on-angularjs-and-rails-8.html?m=1 – angularjslearner66 2014-08-28 20:24:08
只是嘗試一下,如果你沒有成功發佈一些沒有工作的代碼。 – hereandnow78 2014-08-28 20:28:10
hereandnow78,請參閱我的2014年8月29日以粗體編輯(所有後面的代碼,一切都很好地解釋)。 – angularjslearner66 2014-08-29 23:14:42