Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use ng-click on ng-option( or other way to assign value)

How to use ng-options.

$scope.fieldTable = [
    { 
        filed:"text",
        title:"Global"
    },
    {
        field: "relatedentity",
        title: "Entity"
    },
    {
        field:"title",
        title:"Title"
    },
    {
        field: "content",
        title: "Content"
    }
]

I want to build a which use the title as displayed and when select something, popout a alert window which display the according field. The initial selection is

{ 
    filed:"text",
    title:"Global"
}

Can anyone help?

like image 323
Kuan Avatar asked Apr 08 '15 16:04

Kuan


People also ask

What is the difference between Ng-click and Onclick?

Another significant difference between ng-click and onclick is the execution context. Code inside an onclick attribute executes against the global window object, while an expression inside of ng-click executes against a specific scope object, typically the scope object representing the model for the current controller.

How do I set default selected value in ng-options?

Use ng-init to set default value for ng-options . Save this answer. Show activity on this post. In my opinion the correct way to set a default value is to simply pre-fill your ng-model property with the value selected from your ng-options , angular does the rest.

What is the use of NG-click?

The ng-click Directive in AngluarJS is used to apply custom behavior when an element is clicked. It can be used to show/hide some element or it can pop up an alert when the button is clicked.

What is the difference between Ng-options and Ng-repeat?

ng-repeat creates a new scope for each iteration so will not perform as well as ng-options. For small lists, it will not matter, but larger lists should use ng-options. Apart from that, It provides lot of flexibility in specifying iterator and offers performance benefits over ng-repeat. Save this answer.


2 Answers

var app = angular.module('stack', []);

app.controller('MainCtrl', function($scope) {
  $scope.fieldTable = [{
    field: "text",
    title: "Global"
  }, {
    field: "relatedentity",
    title: "Entity"
  }, {
    field: "title",
    title: "Title"
  }, {
    field: "content",
    title: "Content"
  }];

   $scope.selected = $scope.fieldTable[0];

  $scope.hasChanged = function() {
alert($scope.selected.field);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<body ng-app="stack" ng-controller="MainCtrl">
   <select ng-options="item.title for item in fieldTable" ng-model="selected" ng-change="hasChanged()">
  </select>
</body>
like image 188
Bazinga Avatar answered Oct 04 '22 03:10

Bazinga


You can use ng-change to do that

<select ng-options="item.title as item.title for item in fieldTable track by item.title" ng-model="selected" ng-change="onChanged()">

then in your onChange() method in the controller you can do whatever you want it to do :) In your case , show an alert with the value

edit: https://plnkr.co/edit/4csDtFVH5mKd7Xr39WtG?p=preview

like image 36
rmuller Avatar answered Oct 04 '22 03:10

rmuller