以角度js运行控制器

时间:2015-04-01 13:36:13

标签: angularjs

我是Angularjs的新手,所以我做了一个例子来调用控制器,但我无法调用控制器......

这是我的示例代码

<head>

    <meta charset="UTF-8">
    <title>Insert title here</title>
    <script type="text/javascript" src="js/jquery-1.9.0.js"></script>
    <script type="text/javascript" src="angularjs/angular.min.js"></script>
</head>

<body ng-app="">
    <div data-ng-controller="mycontroller">
    </div>
    <script>
        function mycontroller($scope, $http) {
            alert("alert");
        }
    </script>
</body>

2 个答案:

答案 0 :(得分:1)

试试这个

<body ng-app="learning">
    <div data-ng-controller="mycontroller">
    </div>
    <script>
        angular.module("learning", [])
            .controller("mycontroller", function($scope, $http) {
                alert("alert");
            });
    </script>
</body>

答案 1 :(得分:1)

您可以试试这个,但我建议您阅读角度教程,以了解控制器和绑定的工作原理。

http://www.w3schools.com/angular/ https://docs.angularjs.org/tutorial

<html>
<head>
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
</head>
<body>

<div ng-app="myApp"> 
    <div ng-controller="mycontroller" ng-init="initMethod()">
        <!--<input type="text" ng-model="test"></p>
        <div>{{test}}</div>-->
    </div>
</div>
<script>
    var app = angular.module('myApp', []);
    app.controller('mycontroller', function($scope) {
        alert("alert")

        $scope.initMethod = function(){
            alert("alert from init");
        }
    });
</script>
</body>
</html>