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