Home > Web Front-end > JS Tutorial > body text

AngularJS user selector directive example analysis

高洛峰
Release: 2016-12-07 15:59:34
Original
1037 people have browsed it

This article analyzes the AngularJS user selector directive with examples. Share it with everyone for your reference, the details are as follows:

When developing forms, we often need to use the user selector. User data is generally stored in the following way:

User 1, User 2, User 3

We Selectors can be implemented using angular directives.

<!DOCTYPE html>
<html ng-app="app">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  <script src="../assets/js/angular.min.js"></script>
  <link rel="stylesheet" href="../assets/css/bootstrap.min.css">
  <link rel="stylesheet" href="../assets/css/bootstrap-theme.min.css">
  <link rel="stylesheet" href="../assets/css/component.css">
  <link rel="stylesheet" href="../assets/css/form.css">
  <link rel="stylesheet" href="../assets/css/font-awesome.min.css">
   <script src="../assets/js/angular.min.js"></script>
   <script type="text/javascript">
     var base=angular.module("directive",[]);
     base.directive(&#39;htSelector&#39;, function() {
      return {
        restrict : &#39;AE&#39;,
        templateUrl:&#39;selector.html&#39;,
        scope: {
          name: &#39;=name&#39;
        },
        link: function(scope, element, attrs) {
          var aryName=scope.name.split(",");
          scope.names=aryName;
          scope.remove=function(i){
            aryName.splice(i,1);
          };
          scope.$watch(
              "names",
              function (newValue,oldValue) {
                if(newValue!=oldValue){
                  scope.name=aryName.join(",");
                }
              },true
          );
          scope.selectUser=function(){
            aryName.length = 0;
            aryName.push("韩信");
          }
        }
      }
    });
    var app=angular.module("app",["directive"]);
    app.controller(&#39;ctrl&#39;, [&#39;$scope&#39;,function($scope){
      $scope.names=&#39;自由港,马云,刘强东&#39;;
      $scope.getData=function(){
        console.info($scope.names)
      }
    }])
   </script>
</head>
<body ng-controller="ctrl">
  <div ht-selector name="names"></div>
  <button ng-click="getData()">获取数据</button>
</body>
</html>
Copy after login

Template URL

<div>
  <span ng-repeat="item in names">
       {{item}}<a class="btn btn-xs fa-remove" title="移除该项" ng-click="remove($index)"></a>
  </span>
  <a class="btn btn-sm btn-primary fa-search" ng-click="selectUser()">选择</a>
</div>
Copy after login

In the command, an independent scope is used. The incoming data is a comma-delimited string, and we use an array for operation.

The trick here is to convert between strings and arrays.

A command-independent scope is used here, and watch is used to operate the array. It should be noted that if you monitor the array, you need to use in-depth monitoring.


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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!