TypeError:event.attribute不是。$$ ChildScope中的函数。$ childScope。$ scope.Increment1 in angularjs

时间:2017-09-24 08:05:28

标签: javascript angularjs

我是angular.js的初学者  我收到了这个错误:

  

TypeError:event.attribute不是。$$ ChildScope的函数。$$ ChildScope。$ scope.Increment1

浏览器Chrome控制台中的完整错误输出:

> angular.js:11607 TypeError: event.attribute is not a function
>     at a.$$ChildScope.$$ChildScope.$scope.Increment1 (http://localhost:63342/New%20folder%20(2)/index1.html:33:27)
>     at https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:198:303
>     at f (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:214:485)
>     at a.$$ChildScope.$$ChildScope.$eval (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:125:305)
>     at a.$$ChildScope.$$ChildScope.$apply (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:126:6)
>     at HTMLDivElement.<anonymous> (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:215:36)
>     at HTMLDivElement.c (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:32:389)

我的代码:

<!DOCTYPE html>
<html>
<head>
    <title>Demo</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
    <link rel="stylesheet" href="css.css">
</head>
<body>

<div ng-app="app" ng-controller="ClickController">
<div id="bigdiv">
    <div id="div1" data-id="on" ng-click="Increment1($event)"></div>
    <div id="div2" data-id="on" ng-click="Increment1($event)"></div>
    <div id="div3" data-id="on" ng-click="Increment1($event)"></div>
    <br/>
    <div id="div4" data-id="on" ng-click="Increment1($event)"></div>
    <div id="div5" data-id="on" ng-click="Increment1($event)"></div>
    <div id="div6" data-id="on" ng-click="Increment1($event)"></div>
    <br/>
    <div id="div7" data-id="on" ng-click="Increment1($event)"></div>
    <div id="div8" data-id="on" ng-click="Increment1($event)"></div>
    <div id="div9" data-id="on" ng-click="Increment1($event)"></div>
    </div>
    <!--<button ng-click="start()">start</button>-->
    <input id="input1">
</div>
<script>
    var count=0;var m=0;var m1=0;
    var app = angular.module("app", []);
    app.controller("ClickController", function ($scope) {
        $scope.Increment1 = function (event) {
            var id=event.target.id;
            var cc= event.attribute("data-id");
            alert(cc);
            if(count%2==0 ){

            document.getElementById(id).style.backgroundColor = "blue";
                document.getElementById("input1").value ="blue";
                count++;
                m=1;
        }
       else if (count%2==1 ){
                document.getElementById(id).style.backgroundColor = "red";
                document.getElementById("input1").value ="red";
                count++;
                m1=1;

            }
                    }
    });
</script>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

  

TypeError:event.attribute不是。$$ ChildScope的函数。$$ ChildScope。$ scope.Increment1

方法source.mapTo(void 0) ... 没有$event。请参阅attribute的回答

但是在AngularJs中,您可以使用ng-style甚至更好ng-class来使用更简单的方式来设置视图的样式,因为您只使用了2个选项

答案 1 :(得分:0)

而不是使用

var cc= event.attribute("data-id");

只需使用

var cc= event.target.attribute("data-id");