AngularJS 控制器和表达式的行为可以通过以下方法解决:
app.controller
方法。控制器负责管理数据和业务逻辑。var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.message = 'Hello, World!';
});
{{}}
将表达式绑定到视图中。表达式可以是变量、函数调用、运算符等。
{{ message }}
app.controller('myCtrl', function($scope) {
$scope.message = 'Hello, World!';
$scope.updateMessage = function() {
$scope.message = 'New message!';
};
});
{{ message }}
app.controller('myCtrl1', function($scope, sharedDataService) {
$scope.data = sharedDataService.getData();
});
app.controller('myCtrl2', function($scope, sharedDataService) {
$scope.data = sharedDataService.getData();
});
app.service('sharedDataService', function() {
var data = 'Shared data';
this.getData = function() {
return data;
};
});
{{ data }}
{{ data }}
这些方法可以帮助解决 AngularJS 控制器和表达式的行为。