Recently made a selection of the function of the label, put some tags to the user, users choose their favorite tags, similar to what we see on the shopping site of the filter tag like;
Simple effect:
First look at the HTML code:
1 <! DOCTYPE html> 2 Line2 defines the Angularjs App;
Line4 introduction of Angularjs script;
Line5 introduced its own written script2.js script;
LINE7 Specifies the controller Addstylectrl
Line13-15 real-time display of selected labels to the user to see;
Line17-line26 uses a double loop to list the data in the database (in this case, an object stored in the controller);
Line21 This line of code can be very useful: <input type= "checkbox" Id={{tag.id}} name= "{{tag.name}}" ng-checked= "isSelected (tag.id)" ng-click= "Updateselection ($event, tag.id)" >
Stored the tag of the Id,name, using isselected (tag.id) to determine whether to be checked, click Time to Call Updateselection ($event, tag.id) method;
If you want the Ng-click trigger function to get to the element that triggered the function cannot pass directly to this, but need to pass inEVENT。BecauseForInANGULARJSInSurface, this Ground square th< Span id= "mathjax-span-55" class= "Mi" >is Yes Event Because in Angularjs, this is the scope of this place. We can pass in.EVENT,ThenAfter letter Number Tong over event and then gets to that element through Event.target in the function.
LINE29-30 is the test time to yourself, you can see the selected array and the contents of the Selectedtags array;
Then look at the Angularjs code: (script2.js)
1/** 2 * Created by en on 20/05/15. 3 */4//Code goes here 5 6 var iApp = Angular.module ("App", []); 7 8 9 Iapp.controller (' Addstylectrl ', function ($scope) one by one {$scope. tagcategories = [GB] d:1,15 name: ' Color ', Tags: [+] id:1,19 Name: ' Color1 '},21 {id:2,23 name: ' Color2 '},25 {id:3,27 name: ' Color3 ' 28 },29 {id:4,31 name: ' Color4 ' 32},33 ]34},35 {id:2,37 name: ' Cat ', tags:[39 {40 id:5,41 name: ' CAT1 '},43 {ID : 6,45 name: ' Cat2 ' 46},47]48},49 {id:3,51 name: ' Scenario ', 52 tags:[53 {id:7,55 name: ' Home ' 56},57 {id:8,59 name: ' Work ' 60},61]62 }63];64 $scope. selected = [];66 $scope. selectedtags = [];67] var updateselected = function (action,id , name) {if (action = = ' Add ' && $scope. Selected.indexof (id) = =-1) {$scope. Selected.push (ID) ; $scope. Selectedtags.push (name);}73 if (action = = ' Remove ' && $scope. Selected.ind Exof (ID)!=-1) {all var idx = $scope. Selected.indexof (ID); $scope. Selected.splice (idx,1); 76 $scope. Selectedtags.splice (idx,1),}78}79 $scope. updateselection = function ($event, id) {81 var checkbox = $event.target;82 var action = (checkbox.checked? ') Add ': ' Remove '); updateselected (action,id,checkbox.name);}85 $scope. isSelected = function (ID) {87 Return $scope. Selected.indexof (ID) >=0;88}89});
Line6 defines the angular app;
LINE10 defines the controller Addstylectrl;
LINE12-63 defines the Label object
Line64,66 declares the two array objects in the $scope (which can be combined into 1) to store the tag ID and name respectively;
Line68-78 defines the updateselected method, which is called by Updateselection;
LINE69-72: Add data to the array if the add operation and the ' array [id] ' element does not exist (Id,name);
LINE73-77: If the remove operation and the ' array [id] ' element exists, delete the data from the array (id,name);
Line80-84 defines the Updateselection method, which is invoked when the checkbox of an HTML page is clicked;
Line81 gets the clicked DOM element through the $event variable;
Line82 the current state of the checkbox to determine whether it is an add operation or a remove operation;
Line83 call the Updateselected method to update the data;
Line86-88 defines the IsSelected method, which is used to determine whether a checkbox with ID ID is selected, and then passes the value to the ng-checked instruction of the page;
Angularjs Determine if the checkbox/check box is selected and displayed in real time