Angularjs1.5.0-ngcontroller

Source: Internet
Author: User

Looking at the API documentation for ANGULARJS's ngcontroller, I found that the official samples were not the same as usual for my use.

API Portal: Https://docs.angularjs.org/api/ng/directive/ngController

Plunker Address: Http://plnkr.co/edit/b07kfAvLtUhVYtxYUYky?p=preview

How does the official explain the method used by Ng-controller--two ways of declaring it:

1. One binds methods and properties directly onto the controller using this :

Ng-controller = ' SettingsController1 as Settings '

In a rough translation, if there is an error, please correct me: Bind the method and property directly on a controller using this, and make the following declaration on the HTML page: Ng-controller = ' SettingsController1 as Settings '.

Personally, the SettingsController1 defined here is actually equivalent to the JavaScript class.

2. One injects $scope into the Controller:ng-controller = "SettiongsController2"

Ng-controller = "SettiongsController2" is used when $scope is passed into the controller as a value.

So, the second use of Ng-controller is that you can use a JavaScript class as a controller.

The code is as follows:

1. First declare a class in the JS file, and use Angular.module.controller to define a controller.

function SettingsController1() {  this.name = "John Smith";  this.contacts = [    {type: ‘phone‘, value: ‘408 555 1212‘},    {type: ‘email‘, value: ‘[email protected]‘} ];}SettingsController1.prototype.greet = function() {  alert(this.name);};
angular.module(‘controllerAsExample‘, [])  .controller(‘SettingsController1‘, SettingsController1);

2. Use Ng-app and Ng-controller in HTML.

<div id="ctrl-as-exmpl" ng-controller="SettingsController1 as settings">

<label>Name: <input type="text" ng-model="settings.name"/></label>

<button ng-click="settings.greet()">greet</button><br/>

</div>

Angularjs1.5.0-ngcontroller

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.