Reputation: 2551
I am trying to combine two angularjs expression
<div ng-app="">
<p>Name :
<input type="text" ng-model="name"></p>
<h1>Hello {{name}}</h1>
</div>
<div ng-app="">
<p>My first expression: {{ 5 + 5 }}</p>
</div>
by doing this :
<div ng-app="ctrl">
<p>Enter first numb:<input type="text" ng-model="firstnb"/></p>
<p>Enter sec numb:<input type="text" ng-model="secnb"/></p>
<p>Enter operator:<input type="text" ng-model="op"/></p>
<p> Result is : {{firstnb}} {{op}} {{secnb}} = </p>
</div>
but before even calculating the result i am getting this on my browser :
I am a nooby in angular and i have no idea why i am getting the result
note that : <script src="js/angular.min.js"></script>
is loaded perfectly
Upvotes: 0
Views: 235
Reputation: 7615
First of all, you have to declare an Angular app and link it to your HTML:
JS:
var app = angular.module('myApp', []);
HTML:
<html ng-app="myApp">
Then, you have to add a controller to your Angular app and use it in your HTML:
JS:
app.controller('myCtrl', function() {
// Your code here
});
HTML:
<div ng-app="myCtrl">
<p>Enter first numb:<input type="text" ng-model="firstnb"/></p>
<p>Enter sec numb:<input type="text" ng-model="secnb"/></p>
<p>Enter operator:<input type="text" ng-model="op"/></p>
<p> Result is : {{firstnb}} {{op}} {{secnb}} = </p>
</div>
See working JSFiddle
EDIT
I've modified my JSFiddle (see link above) to fit your needs. In the controller:
myApp.controller('MyCtrl', function($scope) {
$scope.updateResult = function() {
console.log($scope.op);
switch($scope.op) {
case '+':
$scope.result = Number($scope.firstnb) + Number($scope.secnb)
break;
}
};
});
I've only implemented the addition, but I'm sure you'll know how to handle the rest
Upvotes: 1
Reputation: 2691
check this one its working :--
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<div ng-app>
<p>Enter first numb:<input type="text" ng-model="firstnb"/></p>
<p>Enter sec numb:<input type="text" ng-model="secnb"/></p>
<p>Enter operator:<input type="text" ng-model="op"/></p>
<p> Result is : {{firstnb}} {{op}} {{secnb}} = </p>
</div>
Upvotes: 0
Reputation: 728
Depending on which version of AngularJS you are using, this is expected.
Check this out may be this will help.
https://docs.angularjs.org/guide/migration#you-can-only-bind-one-expression-to-src-ng-src-or-action-
Upvotes: 0
Reputation: 18576
When you use ng-app="ctrl"
you must also defien the module as well. Also i would suggest not to pollute rootScope, instead create a scope using controller.
angular.module("ctrl",[]);
Else you can just use ng-app
without module name. but It's recommended to use module
name and define it using angular.module
.
<div ng-app>
<p>Enter first numb:<input type="text" ng-model="firstnb"/></p>
<p>Enter sec numb:<input type="text" ng-model="secnb"/></p>
<p>Enter operator:<input type="text" ng-model="op"/></p>
<p> Result is : {{firstnb}} {{op}} {{secnb}} = </p>
</div>
Upvotes: 1