AngularJS the scope value does not display in the views - javascript

I am a newbie in angularjs; the scope value are not displaying in the views.
I have two apps: myApp and ram - each one having their controllers.
I'm trying to display scope values.
Only myApp shows values but ram does not.
<div ng-app="myApp" ng-controller="myCtrl">
First Name: <input type="text" ng-model="firstName"><br>
Last Name: <input type="text" ng-model="lastName"><br>
<br>
Full Name: {{firstName + " " + lastName}}
</div>
<div ng-app="ram" ng-controller="myCtrl2">
First Name: <input type="text" ng-model="firstName2"><br>
Last Name: <input type="text" ng-model="lastName2"><br>
<br>
Full Name: {{firstName2 + " " + lastName2}}
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.firstName= "John";
$scope.lastName= "Doe";
});
</script>
<script>
var app2 = angular.module('ram', []);
app2.controller('myCtrl2', function($scope) {
$scope.firstName2= "ram";
$scope.lastName2= "babu";
});
</script>

You have to bootstrap modules to have multiple ng-app in your page.
<html>
<head>
<script src="angular.min.js"></script>
</head>
<body>
<div id="App1" ng-app="shoppingCart" ng-controller="ShoppingCartController">
<h1>Your order</h1>
<div ng-repeat="item in items">
<span>{{item.product_name}}</span>
<span>{{item.price | currency}}</span>
<button ng-click="remove($index);">Remove</button>
</div>
</div>
<div id="App2" ng-app="namesList" ng-controller="NamesController">
<h1>List of Names</h1>
<div ng-repeat="_name in names">
<p>{{_name.username}}</p>
</div>
</div>
<script>
var shoppingCartModule = angular.module("shoppingCart", [])
shoppingCartModule.controller("ShoppingCartController",
function($scope) {
$scope.items = [
{product_name: "Product 1", price: 50},
{product_name: "Product 2", price: 20},
{product_name: "Product 3", price: 180}
];
$scope.remove = function(index) {
$scope.items.splice(index, 1);
}
}
);
var namesModule = angular.module("namesList", [])
namesModule.controller("NamesController",
function($scope) {
$scope.names = [
{username: "Nitin"},
{username: "Mukesh"}
];
}
);
angular.bootstrap(document.getElementById("App2"),['namesList']);
</script>
</body>
</html>

You can not use two apps on the same page. If you do that then you will need to use Angular Bootstrap.
First assign your second div an id e.g id ="ramID" to something like below
<div id="ramID" ng-app="ram" ng-controller="myCtrl2">
then add this line to your code to your script
angular.bootstrap(document.getElementById("ramID"),['ram']);
hope it helps.

Related

Unable to load 2 angularJS components in sharepoint content editor webparts

I have a requirement where I need to load 2 angular components on the same page.
First I have taken a content editor web part and in the HTML source, I have pasted my angular code where I'm fetching list from SharePoint and then displaying on the page.
My first Custom Editor web part Code:
<div ng-app="myApp" ng-controller="myCtrl">
First Name:
<input type="text" ng-model="firstName">
<br> Last Name:
<input type="text" ng-model="lastName">
<br>
<br> Full Name: {{firstName + " " + lastName}}
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function ($scope) {
$scope.firstName = "John";
$scope.lastName = "Doe";
});
</script>
Now, I have taken a new custom editor web part and almost written the same code with just the property name changes, but I'm unable to bind the angular to the second web part.
My second web part code:
<div ng-app="myApp1" ng-controller="myCtrl1"> First Name:
<input type="text" ng-model="firstName1"/>
<br/> Last Name:
<input type="text" ng-model="lastName1"/>
<br/>
<br/> Full Name: {{firstName1 + " " + lastName1}} </div>
<script>
var app = angular.module('myApp1', []);
app.controller('myCtrl1', function($scope) {
$scope.firstName1 = "John";
$scope.lastName1 = "Doe";
});
</script><br/>
Can anyone tell me the mistake I'm doing here?
I was able to load the first component properly.
I need to load 2 components on the same page.
You cannot use 2 ng-app directives on the same page. This is by design.
However, you can use multiple controllers and initialize your angular component using angular.bootstrap method.
Modify your first webpart code as below:
<div id="myApp" ng-controller="myCtrl">
First Name:
<input type="text" ng-model="firstName">
<br> Last Name:
<input type="text" ng-model="lastName">
<br>
<br> Full Name: {{firstName + " " + lastName}}
</div>
<script>
var myfirstAppDivId = document.getElementById('myApp');
angular.bootstrap(myfirstAppDivId, ['myApp']);
var myfirstApp = angular.module('myApp', []);
myfirstApp.controller('myCtrl', function ($scope) {
$scope.firstName = "John";
$scope.lastName = "Doe";
});
</script>
Modify your second webpart code as below:
<div id="myApp1" ng-controller="myCtrl1"> First Name:
<input type="text" ng-model="firstName1"/>
<br/> Last Name:
<input type="text" ng-model="lastName1"/>
<br/>
<br/> Full Name: {{firstName1 + " " + lastName1}} </div>
<script>
var mysecondAppDivId = document.getElementById('myApp1');
angular.bootstrap(mysecondAppDivId, ['myApp1']);
var mysecondApp = angular.module('myApp1', []);
mysecondApp.controller('myCtrl1', function($scope) {
$scope.firstName1 = "John";
$scope.lastName1 = "Doe";
});
</script><br/>

checkboxes are not displayed inside ng repeat angularjs

I want to display checkboxes inside ng repeat. The checkboxes are not displayed. The checkboxes are not displayed at all. I am not understanding what the misatke is. Here is the code -
<div class = "col s4">
<form ng-submit="$ctrl.todoAdd()">
<input type="text" ng-model="$ctrl.todoInput" size="50" placeholder="Add New">
<input type="submit" value="Add New">
</form>
<br>
<div ng-repeat="x in $ctrl.todoList">
<input type="checkbox" ng-model="x.done"> <span ng-bind="x.todoText"></span>
</div>
<p><button ng-click="$ctrl.remove()">Remove marked</button></p>
</div>
And the javascript code-
angular.
module('dashboard').
component('dashboard', {
templateUrl: 'dashboard/dashboard.html',
controller: ['$routeParams','$http',
function dashboardController($routeParams,$http) {
this.todoList = [{todoText:'Clean House', done:false}];
this.todoAdd = function() {
this.todoList.push({todoText:this.todoInput, done:false});
this.todoInput = "";
};
this.remove = function() {
var oldList = this.todoList;
this.todoList = [];
angular.forEach(oldList, function(x) {
if (!x.done) this.todoList.push(x);
});
};
//Rest of the controller code
}
]
});
You need to have empty dependencies passed to your module while declaring,
change it as,
angular.
module('dashboard',[]).
DEMO
var app = angular.module('myFirstApp', []);
app.controller('myCtrl', function () {
this.todoList = [{
"todoText": "run today",
"done": false
},
{
"todoText": "read today",
"done": false
}];
});
<html>
<head>
<title>My First AngularJS App</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script src="app.js"></script>
</head>
<body>
<h1>My First AngularJS App</h1>
<div ng-app="myFirstApp" ng-controller="myCtrl as ctrl">
<div ng-repeat="x in ctrl.todoList">
<input type="checkbox" ng-model="x.done"> <span ng-bind="x.todoText"></span>
</div>
</div>
</body>
</html>

AngularJS : Variables in ng-model

I have a loop ng-repeat
<div ng-repeat="data in datas">
Name: {{data.name}} <input type="text" ng-model="age">
</div>
I want $scope.age become to $scope.age_data.name. Eg: $scope.age_Tan, $scope.age_Jim...
So i have tried ng-model="age_{{data.name}}" but it make error.
How to solve this?
The "right" way to do this is to, in the controller, do this:
$scope.ages = {};
Then in the template:
Name: {{data.name}} <input type="text" ng-model="ages[data.name]">
Should work...
What you show here won't work exactly here's a couple of options
angular.module('myApp', [])
.controller('MyCtrl', function() {
var vm = this;
vm.datas = [{
name: 'thing1'
}, {
name: 'thing2'
}, {
name: 'thing3'
}];
})
<html ng-app="myApp">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
</head>
<body ng-controller="MyCtrl as myCtrl">
Option 1
<div ng-repeat="data in myCtrl.datas">
<input type="text" ng-model="data.age" />
</div>
<br/>
<br/>Option 2
<div ng-repeat="data in myCtrl.datas">
<input type="text" ng-model="myCtrl.age[data.name]" />
</div>
<pre>{{myCtrl|json}}</pre>
</body>
</html>
I am not sure why you want to keep age of a person in another object/container. Here is the solution which may help you rethink you design.
$scope.people= [
{name: 'Tan', age: 20},
{name: 'Jim', age: 21}
];
<div ng-repeat="person in people">
Name: {{person.name}} <input type="text" ng-model="person.age">
</div>

AngularJS 1.3.8 Using multiple controllers, second controller is not working

How do you use multiple controllers for AngularJS 1.3.8?
I've tried the following below but only the first controller outputs correctly and the second controller outputs with {{ name }} and {{ age }}.
HTML:
<div ng-app="app" ng-controller="Ctrl">
<label>Name:</label>
<input ng-model="name">
<label>Age:</label>
<input ng-model="age">
<h1>{{ name }}</h1>
<h1>{{ age * 2 }}</h1>
</div>
<div ng-app="app2" ng-controller="Ctrl2">
<label>Name:</label>
<input ng-model="name">
<label>Age:</label>
<input ng-model="age">
<h1>{{ name }}</h1>
<h1>{{ age }}</h1>
</div>
<script>
angular.module('app', [])
.controller('Ctrl', ['$scope', function($scope) {
$scope.name = "Jason";
$scope.age = "21";
$scope.$watch('name', function(){ // Logs the amount of times name changes
console.log($scope.name);
});
}]);
</script>
<script>
angular.module('app2', [])
.controller('Ctrl2', ['$scope', function($scope) {
$scope.name = "John";
$scope.age = "22";
}]);
</script>
You cannot have more than 1 ng-app directive in the same document. You would need to manually bootstrap the other. Other wise only the first instance of ng-app will be bootstrapped automatically by angular.
Other issue is that there is no provider called $scope2, you need to inject $scope.
Example:-
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js"></script>
<div ng-app="app" ng-controller="Ctrl">
<label>Name:</label>
<input ng-model="name">
<label>Age:</label>
<input ng-model="age">
<h1>{{ name }}</h1>
<h1>{{ age * 2 }}</h1>
</div>
<div id="app2" ng-controller="Ctrl2">
<label>Name:</label>
<input ng-model="name">
<label>Age:</label>
<input ng-model="age">
<h1>{{ name }}</h1>
<h1>{{ age }}</h1>
</div>
<script>
angular.module('app', [])
.controller('Ctrl', ['$scope',
function($scope) {
$scope.name = "Jason";
$scope.age = "21";
$scope.$watch('name', function() { // Logs the amount of times name changes
console.log($scope.name);
});
}
]);
</script>
<script>
angular.module('app2', [])
.controller('Ctrl2', ['$scope',
function($scope) {
$scope.name = "John";
$scope.age = "22";
}
]);
angular.element(document).ready(function() {
angular.bootstrap(document.getElementById('app2'), ['app2']);
});
</script>
Demo
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js"></script>
<div ng-app="app" ng-controller="Ctrl">
<label>Name:</label>
<input ng-model="name">
<label>Age:</label>
<input ng-model="age">
<h1>{{ name }}</h1>
<h1>{{ age * 2 }}</h1>
</div>
<div id="app2" ng-controller="Ctrl2">
<label>Name:</label>
<input ng-model="name">
<label>Age:</label>
<input ng-model="age">
<h1>{{ name }}</h1>
<h1>{{ age }}</h1>
</div>
<script>
angular.module('app', [])
.controller('Ctrl', ['$scope',
function($scope) {
$scope.name = "Jason";
$scope.age = "21";
$scope.$watch('name', function() { // Logs the amount of times name changes
console.log($scope.name);
});
}
]);
</script>
<script>
angular.module('app2', [])
.controller('Ctrl2', ['$scope',
function($scope) {
$scope.name = "John";
$scope.age = "22";
}
]);
angular.element(document).ready(function() {
angular.bootstrap(document.getElementById('app2'), ['app2']);
});
</script>
If your intention is to use just one module and bind other controllers to it. Then just have one ng-app and register your controller to app1.
Create a module:
angular.module('app', []);
Register a controller:
angular.module('app').controller('Ctrl1', ctor);
Demo
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js"></script>
<div ng-app="app">
<div ng-controller="Ctrl">
<label>Name:</label>
<input ng-model="name">
<label>Age:</label>
<input ng-model="age">
<h1>{{ name }}</h1>
<h1>{{ age * 2 }}</h1>
</div>
<div ng-controller="Ctrl2">
<label>Name:</label>
<input ng-model="name">
<label>Age:</label>
<input ng-model="age">
<h1>{{ name }}</h1>
<h1>{{ age }}</h1>
</div>
</div>
<script>
angular.module('app', [])
.controller('Ctrl', ['$scope',
function($scope) {
$scope.name = "Jason";
$scope.age = "21";
$scope.$watch('name', function() { // Logs the amount of times name changes
console.log($scope.name);
});
}
]).controller('Ctrl2', ['$scope',
function($scope) {
$scope.name = "John";
$scope.age = "22";
}
]);;
</script>
Why not just define 2 controllers for 1 app?
<!DOCTYPE html>
<html>
<head>
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
</head>
<body ng-app="myApp" >
<p>Person</p>
<div ng-controller="myCtrl">
First name <input type="text" ng-model="firstName"><br>
Last name <input type="text" ng-model="lastName"><br>
Birthday <input type="date" ng-model="birthday"><br>
{{firstName+' '+lastName}} was born on {{birthday}}
</div>
<div ng-controller="myCtrl2" ng-init="">
<p>The third result is {{ points[2] }}</p>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.firstName= "John";
$scope.lastName= "Doe";
$scope.birthday= new Date();
});
app.controller('myCtrl2', function($scope) {
$scope.points=[1,15,19,2,40];
});
</script>
</body>
</html>

how to create object while using ng-repeat and array in angularjs

Hello Everyone I have face a problem to create a object with ng-repeat. when i declare the Object then the same value in filled in the text box which has same ng-model value. if i am not Declare the object then duplicacy is not occures.
If i am declare the $scope.user = {}; in js file then problem is occures. please check this. Give me a solution ASAP.
My Fiddle Link Please Check This http://jsfiddle.net/Ladjkp5s/1/
Here is my Html file
<div ng-app="myApp">
<div ng-controller='MainController' ng-init="start = 0; end = 5;">
Start: <input ng-model="start"> End: <input ng-model="end">
<ul>
<li ng-repeat="item in items | slice:start:end">{{item + 1}}
<div>
Name: <input type="text" ng-model="user.name"><br>
Address: <input type="text" ng-model="user.add"><br>
Phone: <input type="text" ng-model="user.phn"><br>
ZipCode: <input type="text" ng-model="user.zip">
</div>
</li>
</ul>
</div>
</div>
Here is my JS File
var app = angular.module('myApp', []);
app.filter('slice', function() {
return function(arr, start, end) {
return arr.slice(start, end);
};
});
app.controller('MainController', function($scope) {
$scope.items = [];
$scope.user ={};
for (var i = 0; i < 5; i++) $scope.items.push(i);
});
Thanks.
The problem is that you are using same ng-model for every item.(user.fieldName) you have to use item.fieldName.
<div ng-app="myApp">
<div ng-controller='MainController' ng-init="start = 0; end = 5;">
Start: <input ng-model="start"> End: <input ng-model="end">
<ul>
<li ng-repeat="item in items | slice:start:end">{{item.index + 1}}
<div>
Name: <input type="text" ng-model="item.name"><br>
Address: <input type="text" ng-model="item.add"><br>
Phone: <input type="text" ng-model="item.phn"><br>
ZipCode: <input type="text" ng-model="item.zip">
</div>
</li>
</ul>
</div>
</div>
http://jsfiddle.net/3akwk7tv/
Yuu can not loop in controller function, but u can create controller that loops in html like in this example
<ul>
<li ng-repeat="theItem in myData.items">{{theItem.text}}</li>
</ul>
<script>
angular.module("myapp", [])
.controller("MyController", function($scope) {
$scope.myData = {};
$scope.myData.items = [ {text : "one"}, {text : "two"}, {text : "three"} ];
});
</script>

Categories