我是AngularJS的新手。我想通過加減兩個數字來將模板返回給指令。我正在通過$scope
功能,但它不工作。
我從Angular Modules with Directive
這裏學習是代碼:
<html>
<head>
<title>Angular JS </title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
</head>
<body>
<div ng-app="Compl">
<input type=number ng-model="no1" placeholder="Enter the First Number" />
<input type=number ng-model="no2" placeholder="Enter the First Number" />
<NoSum></NoSum>
<NoSub></NoSub>
</div>
<script>
var app = angular.module("Compl", []);
app.directive("NoSum", function($scope) {
return {
template: "Sum of Two Number" + ($scope.no1 + $scope.no2)
};
});
app.directive("NoSub", function($scope) {
return {
template: "Sub of Two Number" + ($scope.no1 - $scope.no2)
};
});
</script>
</body>
</html>
您無法直接在您的指令中傳遞$ scope。相反,創建一個鏈接功能和使用範圍。欲瞭解更多詳情https://docs.angularjs.org/guide/directive – Vivz