2016-06-17 4 views
1
Js 

var myApp = angular.module("DataEntryApp", []); 

myApp.controller("DataEntryController",function($scope) { 

    alert("totalPrice?" + $scope.totalPriceAmt); 
} 

$scope.Add = function() { 
     alert("Total amount commint:" +$scope.totalPriceAmt);  
}; 

Html Seite html inwie berechnete Werte (totalPriceAmt) von js erhalten durch Verwendung Winkel js

Ich bin nicht in der Lage, die Werte totalPriceAmt in HTML-Seite zu erhalten, nachdem auf submit Schaltfläche klicken oder Ich möchte totalPrice in HTML direkt anzeigen.

+0

den Umfang Variable in Alert Einlochen initialisiert nicht den Wert :), versuchen Sie den Wert wie die Zuordnung '$ scope.totalPriceAmt = XXX;' – MasterMohsin

Antwort

0

Wenn Sie den $ scope-Wert in html anzeigen möchten, müssen Sie diesem einen Wert zuweisen. Wie ich aus deinem Code ersehen kann, findet keine Wertangabe statt.

var myApp = angular.module("DataEntryApp", []); 
 

 
myApp.controller("DataEntryController",function($scope) { 
 
    $scope.totalPriceAmt = 10; 
 
} 
 

 
$scope.Add = function() { 
 
    $scope.totalPriceAmt = 10; 
 
};

<div ng-app="DataEntryApp" ng-controller="DataEntryController"> 
 
     <button ng-click="Add()">Submit</button> 
 
     <span>Total: {{ totalPriceAmt }} </span> 
 
</div>

Nun, wenn Sie die Anwendung ausführen, nachdem die Schaltfläche klicken, wird es funktionieren.

0

Hey, Sie müssen die Variable mit der ng-bind binden und dann Zugriff auf den Controller geben und den $ scope verwenden und die Operation ausführen.

1

gibt es Syntaxfehler Funktion außerhalb Controller definiert ist

var myApp = angular.module("DataEntryApp", []); 

myApp.controller("DataEntryController",function($scope) { 

$scope.totalPriceAmt =10; 


$scope.Add = function() { 
$scope.totalPriceAmt =$scope.totalPriceAmt+10; 
}; 


}); 

http://codepen.io/vkvicky-vasudev/pen/xOOBGq

0

Es gibt einen Syntaxfehler für die Registrierung der Controller mit der Winkel Anwendung in Ihrem Beispiel war. Im beigefügten Code-Snippet können Sie sehen, dass ich den Wert totalPriceAmount auf 0 im Controller initialisiere und ihn dann jedes Mal um 10 inkrementiere, wenn die Methode Add aufgerufen wird, wenn Sie auf die Schaltfläche klicken. Sie sehen auch, dass die Datenbindung an die Bereichsvariable ebenfalls korrekt erfolgt.

var myApp = angular.module("DataEntryApp", []); 
 

 
myApp.controller("DataEntryController", function($scope) { 
 
    $scope.totalPriceAmt = 0; 
 
    
 
    $scope.Add = function() { 
 
    $scope.totalPriceAmt += 10; 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js"></script> 
 

 
<div ng-app="DataEntryApp" ng-controller="DataEntryController"> 
 
    <button ng-click="Add()">Submit</button> 
 
    <span>Total: {{ totalPriceAmt }} </span> 
 
</div>

+0

danke allen von euch, ich habe es – shivaji