在某一點的角度獲取數據。用戶看文本。那麼發生了什麼?
@greenoldman AngularJS使用interpolate service做數據綁定。
下面是一個插值字符串例子Pro AngularJS作者:Adam Freeman,我希望下面的例子能幫助你。
angular
.module("exampleApp", [])
.controller("defaultCtrl", defaultCtrl)
.directive("evalExpression", evalExpression);
function defaultCtrl($scope) {
$scope.dataValue = "100.23";
}
function evalExpression($interpolate) {
var interpolationFn = $interpolate("The total is: {{amount | currency}} (including tax)");
return {
scope: {
amount: "=amount",
tax: "=tax"
},
link: function(scope, element, attrs) {
scope.$watch("amount", function (newValue) {
var localData = {
total: Number(newValue) + (Number(newValue) * (Number(scope.tax) /100))
}
element.text(interpolationFn(scope));
});
}
}
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="exampleApp">
<div ng-controller="defaultCtrl">
<div class="well">
<p><input class="form-control" ng-model="dataValue" /></p>
<div>
<span eval-expression amount="dataValue" tax="10"></span>
</div>
</div>
</div>
</div>
Interpolation example with people data
<div ng-app="demo">
<div ng-controller="DefaultController as ctrl">
<h1>Persons</h1>
<div ng-repeat="person in ctrl.persons">
<person-details person="person"></person-details>
</div>
</div>
</div>
angular
.module('demo', [])
.controller('DefaultController', DefaultController)
.directive('personDetails', personDetails);
function DefaultController() {
var vm = this;
vm.persons = [
{ id: 1, name: 'Name 1', age: 24, address: 'Address Line 1' },
{ id: 2, name: 'Name 2', age: 22, address: 'Address Line 2' },
{ id: 3, name: 'Name 3', age: 23, address: 'Address Line 3' }
];
}
function PersonController() {
}
personDetails.$inject = ['$interpolate'];
function personDetails($interpolate) {
debugger
var interpolationFn = $interpolate('{{person.name}} {{person.age}} {{person.address}}');
var directive = {
restrict: 'E',
scope: {
person: '=person'
},
link: link,
controller: PersonController,
controllerAs: 'vm',
bindToController: true
};
return directive;
function link(scope, element, attrs, ngModelCtrl) {
debugger
scope.$watch("vm.person", function (newValue, oldValue) {
var value = interpolationFn(scope.vm);
element.prop('innerHTML', value);
});
}
}
Interpolation example with people data (using only AngularJS Controller)
<div ng-app="demo">
<div ng-controller="DefaultController as ctrl">
<h1>Persons</h1>
<div ng-repeat="person in ctrl.persons">
{{ctrl.printPersonDetails(person)}}
</div>
</div>
</div>
angular
.module('demo', [])
.controller('DefaultController', DefaultController);
DefaultController.$inject = ['$interpolate'];
function DefaultController($interpolate) {
var vm = this;
var interpolationFn = $interpolate('{{name}} {{age}} {{address}}');
vm.persons = persons;
vm.printPersonDetails = printPersonDetails;
function printPersonDetails(person) {
return interpolationFn(person);
}
}
var persons = [
{ id: 1, name: 'Name 1', age: 24, address: 'Address Line 1' },
{ id: 2, name: 'Name 2', age: 22, address: 'Address Line 2' },
{ id: 3, name: 'Name 3', age: 23, address: 'Address Line 3' }
];
非常感謝您提供這樣豐富的答案! – greenoldman