Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/432.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ng单击不起作用-AngularJs_Javascript_Angularjs_Html_Angularjs Ng Click - Fatal编程技术网

Javascript ng单击不起作用-AngularJs

Javascript ng单击不起作用-AngularJs,javascript,angularjs,html,angularjs-ng-click,Javascript,Angularjs,Html,Angularjs Ng Click,我已经检查了这里所有可用的解决方案,但对我没有帮助 我试图显示一个从AngularJs到html的按钮,但ng click不适用于或标记。我在Eclipse中工作,它包含许多内置模块。请帮帮我。下面是示例代码: <!DOCTYPE html> <html> <head> <script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js">

我已经检查了这里所有可用的解决方案,但对我没有帮助

我试图显示一个从AngularJs到html的按钮,但ng click不适用于或标记。我在Eclipse中工作,它包含许多内置模块。请帮帮我。下面是示例代码:

   <!DOCTYPE html>
<html>
<head>
    <script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
</head>
<body>
<div ng-app="" ng-controller="contrl">
<a href="" ng-click="testFunc1()">Link1</a>
<div id="id1">Text1</div>
<p id="id2">Text2</p>
</div>

<script>
    function contrl($scope,$http,$location){
            xyz = '<button type="submit" ng-click="testFunc2()">Link2</button>';
            document.getElementById("id1").innerHTML = xyz;

            $scope.testFunc1 = function() {
            document.getElementById("id2").innerHTML = "abc";
            };
            $scope.testFunc2 = function() {
            document.getElementById("id2").innerHTML = "xyz";
            };
    }
</script>
</body>
</html>

当我通过angularJs时,只有ng click属性不起作用。甚至href属性也适用于相同的情况。当我使用Eclipse时,所有需要的模块都已经在不同的文件中设置好了。我和angularJs合作了两周,除了我遇到这个问题外,一切都正常。

不完全确定你在做什么,但像这样的事情会更好:

<!DOCTYPE html>
<html>
<body>
<div ng-app="myApp" ng-controller="contrl">
<p><a href="" ng-click="testFunc1()">Link1</a></p>
<div id="id1">{{ val1 }}</div>
<p id="id2">{{ val2 }}</p>
</div>

<script>
  var myApp = angular.module('myApp',[]);

  myApp.controller('contrl', ['$scope', '$http', '$location', function($scope, $http, $location) {

      $scope.val1 = "text1";
      $scope.val2 = "text2";

      $scope.testFunc1 = function(){
            $scope.val1 = 'New Value';              
      }

      $scope.testFunc2 = function(){      
            $scope.val2 = 'Second New Value';                         
      };
  }]);

</script>
</body>
</html>

你的代码是人们会遵循的最基本的方法。这没有错,唯一的问题是你似乎正在使用angular的更新版本进行开发,或者你忘记了包含任何代码

最近发布的angular版本不支持您的应用程序开发模式

我一直使用到1.2.26,有效

您还使用了innerHTML以及document.getElementById


这些根本不是必需的。阅读更多关于绑定和angular的其他最佳部分的信息。

这是因为angular不知道使用普通javascript带来的ng点击。 因此,如果您想在html中执行此操作,可以这样编写脚本:

    function contrl($scope){


        $scope.text = 'text2';
        $scope.testFunc1 = function() {
            $scope.text = "abc";
        };
        $scope.testFunc2 = function() {
            $scope.text = "xyz";
        };
};

如果您仍想从脚本中插入html,请查看此处:

是否可以添加少量此代码?在何处声明应用程序和控制器?我同意@JsIsAwesome。您尚未正确设置Angular应用程序或控制器。看一看是的。除非您正确设置了angular应用程序,否则此操作将不起作用。我看不到您在哪里加载Angular min文件和所有文件?我已经在示例中包括Angular min文件。我正在使用它,但没有复制到这里。不管怎样,结果都是一样的,ng click不起作用。显然,您需要包含调用才能获得角度库!我的任务是将带有ng click属性的按钮或锚定标记从angularJs发送到html。你的例子解决不了我的目的。是的,但为什么?我们正在努力实现什么?您是否可以使用angular变量通过ng show显示/隐藏dom元素,而不是从angular将HTML推入dom?我的要求是显示链接锚定或按钮标记的“n”个数,我将从数据库中获取“n”。由于“n”不是一个确定的数字,我无法在html中定义特定数量的链接。首先,我尝试用这种方式打印至少一个链接。在您的演示中,您也为div元素提供了ng click=testFunc2。因此,它起了作用。一旦我从div中删除ng click,它就不会工作了。