7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Getting Started - AngularJS

Posted at

参考リンク

セットアップ

Gemfile

gem 'angularjs-rails'
bundle install

app/assets/javascripts/application.js

//= require angular

app/views/layouts/application.html.haml

%html{ 'ng-app' => true }

app/views/posts/_form.html.haml

%div
  %label Name:
  %input{ "ng-model" => "yourName", "ng-init" => "yourName='You'", placeholder: "Enter a name here", type: "text" }
  %h1 Hello {{yourName}}!
The only appropriate use of ngInit for aliasing special properties of ngRepeat, as seen in the demo below. Besides this case, you should use controllers rather than ngInit to initialize values on a scope.

コントローラ

  • 文字列をクリックする度、末尾に「Hello」を追加する
function firstCtrl($scope){
  $scope.hello = "Hello Angular!";

  $scope.like_countup = function() {
    $scope.hello = $scope.hello + " Hello";
  };
};
%div{ "ng-controller" => "firstCtrl" }
  %p{ "ng-click" => "like_countup()" } {{hello}}
  • カウントアップ
function likeCtrl($scope){
  $scope.like_count = 1;

  $scope.like_countup = function() {
    $scope.like_count += 1;
  };
};
%p{ "ng-controller" => "likeCtrl" }
  %button{ type: 'button', "ng-click" => "like_countup()" } いいね
  %span x{{like_count}}
  %br
7
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?