Simple example of AngularJS ng controller instruction
- 2021-07-06 10:12:36
- OfStack
AngularJS ng-controller Directive
AngularJS instance
Add a controller for the application variable:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
</head>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
Full Name: {{firstName + " " + lastName}}
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.firstName = "John";
$scope.lastName = "Doe";
});
</script>
<p> This example demonstrates how to define a 1 Controllers and scope The use of. </p>
</body>
</html>
Run results:
Full Name: John DoeThis example demonstrates how to define a controller and the use of scope.
Definition and usage
The ng-controller directives are used to add controllers to your application.
In the controller, you can write code, make functions and variables, and use scope objects to access them.
Grammar
<
element ng-controller="expression"
>
<
/element
>
All HTML elements are supported.
Parameter value
值 | 描述 |
---|---|
expression | 控制器名。 |
I hope to help students who learn AngularJS, and continue to supplement relevant knowledge in the future.