最近の更新

2016年10月12日水曜日

コントローラにオブジェクトを作成する方法

【目的】
AngularJSで、コントローラにオブジェクトを作成します。



【手順】
1.『AngularJS1.5.8のダウンロード方法』の手順で、AngularJS1.5.8をダウンロードしておきます。
2.『index.html』というファイルを作成。
3.『index.html』を以下の様に入力。
<!doctype html>
<html ng-app="mainModule">
    <head>
        <script src="angular.min.js"></script>
        <script>
            var mainModule = angular.module('mainModule',[]);
            var sampleController = function($scope) {
                $scope.person = {
                    name: 'あいうえお',
                    age: 20,
                    info: function() {
                        return "名前:" + this.name + ", 年齢:" + this.age;
                    }
                };
            };
            mainModule.controller('sampleController', ['$scope', sampleController]);
        </script>
    </head>
    <body ng-controller="sampleController">
        名前:{{person.name}}<br />
        年齢:{{person.age}}<br />
        情報:{{person.info()}}<br />
    </body>
</html>

4.ダウンロードした『angular.min.js』と『index.html』を同一ディレクトリに配置します。
5.『index.html』をブラウザで開きます。
6.以下の様に表示されれば成功です。













































以上です。

0 件のコメント:

コメントを投稿

注: コメントを投稿できるのは、このブログのメンバーだけです。

関連記事