2016-07-13 2 views
0

Rails에있는 배열을 각도에서 가져 오거나 복사하거나 액세스 할 수 있습니까? 내 def new 함수에 @array_of_names라는 문자열 배열이 있고 내 각도 함수에 동일한 배열 (@array_of_names)을 갖고 싶습니다. 그래서 Angular 측에서 @array_of_names를 가질 수있는 방법은 무엇입니까?Rails에있는 배열을 각도에서 가져 오거나 복사하거나 액세스 할 수 있습니까

레일 당신은 AJAX를 통해이 작업을 수행 할 수

def new 
    @account = Account.new 
    @array_of_names = read_names() 
end 

각도 파일

(() => { 

    angular 
    .module('accounts') 
    .controller('AccountsController', AccountsController) 

    AccountsController.$inject = [] 

    //testing function 
    function AccountsController() { 
    let vm = this; 
    vm.claim = "Hello, world!"; 
    } 

})() 
+0

사용'로 작업하는 방법을 설명하는 링크입니다 $ http'가 아약스 요청을하고 레일스가 json 응답을 보내도록했습니다 – charlietfl

+0

@charlietfl, 저는 최근에 레일을 시작했습니다. 이것은 각도 경험의 첫날입니다. 평균? – user6465508

+0

https://docs.angularjs.org/api/ng/service/$http – charlietfl

답변

0

파일. 레일즈 애플리케이션에서 Angular가 사용할 JSON 객체를 반환하는 경로를 설정해야합니다.

def get_names 
    render json: read_names() 
end 

경로 (/config/routes.rb :

레일
컨트롤러 이름

컨트롤러 (/app/controllers/controller_name.rb)와 controller_name 교체) :

get '/get_names' => 'controller_name#get_names' 

각도

function AccountsController($http){ 

    var vm = this; 

    $http.get('/get_names').then(function(response){ 
    vm.names = response.data; 
    }); 
} 

AccountsController.$inject = ['$http']; 

정말하고있는 각 이해하려면이 링크를 참조하십시오 : 여기

https://docs.angularjs.org/api/ng/service/$http은 레일 및 더 복잡한 JSON 응답 https://www.leighhalliday.com/responding-with-json-in-rails

관련 문제