Ng模型没有按预期工作?

时间:2017-03-27 11:15:10

标签: javascript html angularjs model-view-controller angular-ngmodel

我有一个文本框,它与ng-model=sampleValue

的角度js相连

现在,我的要求是每当我点击按钮时,应该执行show函数并且文本框的值应该更改为Harish。但这不是我得到的结果。请看一下我在下面写的代码:



var app = angular.module("myApp", [])

app.controller("mainController", [ "$scope", function($scope){
  $scope.show = function(){
    $scope.sampleValue = "Harish";
  }
}]);

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body ng-app = "myApp">
<input ng-controller="mainController" type="text" ng-model="sampleValue"/>
<button ng-click="show()">Click Here</button>
</body>
&#13;
&#13;
&#13;

提前谢谢你:)

7 个答案:

答案 0 :(得分:3)

目前您已在输入元素上应用ng-controller,这意味着控制器范围仅限于input DOM,因为show方法无法调用。您应该将ng-controller移动到包装器元素(此处为body)。

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js">  </script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body ng-app="myApp" ng-controller="mainController">

  <input type="text" ng-model="sampleValue" />
  <button ng-click="show()">Click Here</button>
</body>

Plunker

答案 1 :(得分:2)

将ng-controller移动到body标签:

&#13;
&#13;
var app = angular.module("myApp", [])

app.controller("mainController", [ "$scope", function($scope){
  $scope.show = function(){
    $scope.sampleValue = "Harish";
  }
}]);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body ng-app = "myApp"  ng-controller="mainController">
<input type="text" ng-model="sampleValue"/>
<button ng-click="show()">Click Here</button>
</body>
&#13;
&#13;
&#13;

答案 2 :(得分:1)

ng-controller放入正文或某个父div。您在ng-controller

中使用input type

试试这个

&#13;
&#13;
<!DOCTYPE html>
<html ng-app="myApp">

<head>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js"></script>
	
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
	<script type="text/javascript">

		angular.module('myApp',[])
		.controller('myCtrl', function myCtrl($scope,$filter) {


			$scope.showValue = function(){
				$scope.sampleValue = "Harish";
			}
		})



	</script>

</head>

<body ng-controller="myCtrl">
	<input type="text" ng-model="sampleValue"/>
	<button ng-click="showValue()">Click Here</button>
</body>

</html>
&#13;
&#13;
&#13;

答案 3 :(得分:1)

var app = angular.module("myApp", [])

app.controller("mainController", [ "$scope", function($scope){
  $scope.show = function(){
    $scope.sampleValue = "Harish";
  }
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body ng-app = "myApp" ng-controller="mainController">
<input  type="text" ng-model="sampleValue"/>
<button ng-click="show()">Click Here</button>
</body>

您的ng-controller在输入

答案 4 :(得分:1)

事实是你使用ng-controller而不是输入标签。所以在控制器功能内部的输入标签之外将不起作用。你的js完全没问题。你只需要改变html

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <body ng-app = "myApp" >
    <input  type="text" ng-model="sampleValue"/>
    <button ng-click="show()">Click Here</button>
    </body>

答案 5 :(得分:0)

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
    <title>Ang</title>
</head>
<body>
<div ng-app="">
  <h1>Ng-show  ng-hide</h1>
  <p class="description">Click on the "show"-link to see the content.</p>
  <a href="" ng-click="showme=true">Show</a>
  <button ng-click="showme=false">Hide</button> 

  <div class="wrapper">
    <p ng-hide="showme">It will appear here!</p>
    <h2 ng-show="showme">This is mah content, yo!</h2>
  </div>

</div>    

</body>
</html>

尝试执行上述代码,您可能会对代码有所了解。

答案 6 :(得分:0)

您的控制器范围仅限于输入文本框。

   <body ng-app = "myApp">
      <div ng-controller="mainController">
      <input  type="text" ng-model="sampleValue"/>
      <button ng-click="show()">Click Here</button>
      </div>
   </body>