最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Simple ng-click not working in typescript - Stack Overflow

programmeradmin3浏览0评论

I'm trying to call a function in click function from my html page , added all typescript definition files from nuget but something is going wrong My Click Function is not Working .... No error in console even

Here is my Hrml and Controller Code

Html Page

<!DOCTYPE html>
<html xmlns="">


<head>
    <script src="Scripts/angular.js"></script>

    <script src="test.js"></script>
    <title></title>

</head>
<body ng-app="testModule">
    <div ng-controller="test">
        <input type="button" ng-click="click()" value="test" />
    </div>
</body>
</html>

Controller

angular.module("testModule", []);

class test {

    constructor() { }

    click() {

        alert();
    }
} 


angular.module("testModule").controller("test", test );

I'm trying to call a function in click function from my html page , added all typescript definition files from nuget but something is going wrong My Click Function is not Working .... No error in console even

Here is my Hrml and Controller Code

Html Page

<!DOCTYPE html>
<html xmlns="http://www.w3/1999/xhtml">


<head>
    <script src="Scripts/angular.js"></script>

    <script src="test.js"></script>
    <title></title>

</head>
<body ng-app="testModule">
    <div ng-controller="test">
        <input type="button" ng-click="click()" value="test" />
    </div>
</body>
</html>

Controller

angular.module("testModule", []);

class test {

    constructor() { }

    click() {

        alert();
    }
} 


angular.module("testModule").controller("test", test );
Share Improve this question asked Apr 6, 2016 at 12:52 sudhirsudhir 1,4374 gold badges26 silver badges45 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 11

This does not work because ng-click="click()" tries to call $scope.click() which is not defined.

I would highly advise you to use the controller as-Syntax when working with AngularJS and Typescript

Demo

Here is the corrected code. Don't mark this as the answer, @Aides got here before me.

Html Page

<!DOCTYPE html>
<html> <!-- its 2016 -->
<head>
   <script src="Scripts/angular.js"></script>

   <script src="test.js"></script>
   <title></title>

</head>
<body ng-app="testModule">
   <div ng-controller="Test as test">
       <input type="button" ng-click="test.click()" value="test" />
   </div>
</body>
</html>

Controller

angular.module("testModule", []);

class Test {

    click() {
        alert();
    }
} 

angular.module("testModule").controller("Test", Test );
发布评论

评论列表(0)

  1. 暂无评论