Home > Web Front-end > JS Tutorial > Sharing of input examples in angularJS

Sharing of input examples in angularJS

高洛峰
Release: 2016-12-28 16:50:01
Original
1007 people have browsed it

Here I will share with you an example of using the input command

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
</head>
<script src="http://localhost:81/js/jquery.js">
</script>
<script src="http://localhost:81/js/angular.min.js">
</script>
<body ng-app="Demo">
<div ng-controller="TestCtrl">
    <input type="text" ng-model="a" test />
    <button ng-click="show(a)">查看</button>
</div>
</body>
<script>
   var app = angular.module(&#39;Demo&#39;, [], angular.noop);
   app.directive(&#39;test&#39;, function(){<br>   //input 指令的 link有第四个参数,$ctrl有些方法,你可以自己拿来用
     var link = function($scope, $element, $attrs, $ctrl){
    console.log( $ctrl )
       $ctrl.$formatters.push(function(value){
         return value.join(&#39;,&#39;);
       });
       $ctrl.$parsers.push(function(value){
         return value.split(&#39;,&#39;);
       });
     }
     return {compile: function(){return link},
             require: &#39;ngModel&#39;,
             restrict: &#39;A&#39;}
   });
   app.controller(&#39;TestCtrl&#39;, function($scope){
     $scope.a = [];
     //$scope.a = [1,2,3];
     $scope.show = function(v){
       console.log(v);
     }
   });
</script>
</html>
Copy after login

The code is very simple, friends can expand it freely, I hope you can like it

More related input examples in angularJS Please pay attention to the PHP Chinese website for articles!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template