angularjs embed with visualforce page - angularjs

I am try to embed an angular js script within VF page.But I am getting two errors:
/*ajpage:16 Uncaught SyntaxError: Unexpected token . angular.min.js:40 Uncaught Error: [$injector:modulerr] http://errors.angularjs.org/1.5.8/$injector/modulerr?p0=app&p1=Error%3A%20%…ogleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.5.8%2Fangular.min.js%3A20%3A390)
ajpage:
apex:page doctype="html-5.0" sidebar="false" showHeader="false">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
<div ng-app = "app" ng-controller="myCtrl">
<p>Please upload you file:</p>
<p><input type="file" ng-model="allText"/></p>
<button ng-click="readCSV()">Upload</button>
<button ng-click="extractfile()">test</button>
</div>
<div ng-repeat="f in constr">
{{constr}}
</div>
<script>
var app = angular.module('app',[]);
.controller('myCtrl', myCtrl);
function myCtrl($scope) {
$scope.$log = $log;
$scope.readCSV = function($scope) {
var allTextLines = $scope.allText.split(/\r\n|\n/);
for ( var i = 0; i < allTextLines.length; i++)
{
var tarr = [];
tarr.push(allTextLines[i]);
}
};
$scope.extractfile = function(tarr) {
var constr[];
var deployobj ={
"Componenttype": Componenttype,
"ComponentApiname":ComponentApiname,
};
deployobj.push(tarr);
for (var j=0;j<$scope.deployobj.length;j++)
{
var str1 = "<members>";
var str2 = "</members>";
if ($scope.deployobj[j].Componenttype=== customobject)
{
var result = str1 +" "+$scope.deployobj[j].Componenttype+" "+str3;
$scope.constr.push(result);
}
}
};
}
</script>
</apex:page>
Can anyone help me to resolve this issue.

Try to this way .
var app = angular.module('app',[]);
.controller('myCtrl', function(){
//All the functionalities are inject here.
});

<apex:page doctype="html-5.0" sidebar="false" showHeader="false">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
<div ng-app = "app" ng-controller="myCtrl">
<p>Please upload you file:</p>
<p><input type="file" ng-model="allText"/></p>
<button ng-click="readCSV()">Upload</button>
<button ng-click="extractfile()">test</button>
</div>
<div ng-repeat="f in constr">
{{constr}}
</div>
<script>
var app = angular.module('app',[]);
.controller('myCtrl', function(){
$scope.readCSV = function($scope) {
var allTextLines = $scope.allText.split(/\r\n|\n/);
for ( var i = 0; i < allTextLines.length; i++)
{
var tarr = [];
tarr.push(allTextLines[i]);
}
};
$scope.extractfile = function(tarr) {
var constr[];
var deployobj ={
"Componenttype": Componenttype,
"ComponentApiname":ComponentApiname,
};
deployobj.push(tarr);
for (var j=0;j<deployobj.length;j++)
{
var str1 = "<members>";
var str2 = "</members>";
if ($scope.deployobj[j].Componenttype=== customobject)
{
var result = str1 +" "+deployobj[j].Componenttype+" "+str3;
$scope.constr.push(result);
}
}
};
});
</script>
</apex:page>
Changed code

Related

How to use angular.bind(self, fn, args)

I am new to angular js. I was gone through the angular api references,
I had seen function called angular.bind(self, fn, args).
I could not understand the usage of this function. Can anyone explain this function with one example?
It used in Function Currying. An example with JavaScript:
var concat = function(input1) {
return function(input2) {
console.log(input1 + ", " + input2);
};
};
var externalFunction = concat("Hello");
externalFunction("World!"); // gives: "Hello, World!"
This allows you to use only some parameters and not all, for example concant("Hello") instead of concant("Hello","World!"). You can imagine using a defined variable as one of the parameters, whereas you fill in the second one from a user input. The same concept can be used with AnuglarJS:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular.min.js"></script>
</head>
<body ng-app="app">
<div ng-controller="bindController">
<input type="number" ng-model="num" ng-change="AddValue()" />
Adding 5: {{Add}}
</div>
</body>
</html>
<script>
var app = angular.module("app", []);
app.controller('bindController',['$scope',function($scope){
$scope.num = 30;
$scope.AddValue = function () {
var addData = angular.bind(this, function (a, b) {return a + b;});
$scope.Add = addData(5, $scope.num);
}
$scope.AddValue();
}]);
</script>
Controller
app.controller('Identity`enter code here`Controller', ['$scope',
function($scope) {
$scope.Name = "";
$scope.Result = "";
var greet = function (greeting, punctuation) {
$scope.Result = greeting + ' ' + $scope.Name +''+ punctuation;
};
$scope.callBind = function () {
var bound = angular.bind(this, greet, 'Welcome');
bound('!!!!!');
};
}]);
html
<fieldset style="background-color:#DDE4E9;">
<legend>AngulerJS $scope.bind() Example</legend>
<div ng-controller="IdentityController">
<p>{{Result}}</p>
<input ng-model="Name">
<button ng-click="callBind()">Call Bind</button>
</div>
</fieldset>
I Think Its Working

How to wrap this controller logic inside a angular function and use it in select html element

How to wrap this controller logic inside a angular function and use it in select html element. Here inside controller code is use as inline logic without function.
Controller:
var year = new Date().getFullYear();
var range = [];
range.push(year);
for(var i=1;i<20;i++) {
range.push(year + i);
}
$scope.years = range;
View:
<select ng-options="year for year in years">
</select>
You can declare a function like that :
$scope.myfunction = function(){
var year = new Date().getFullYear();
var range = [];
range.push(year);
for(var i=1;i<20;i++) {
range.push(year + i);
}
return range;
}
and in your HTML :
<select ng-model="selectedYear" ng-options="year for year in myfunction()"></select>
I think all you are missing is the ng-model from the select statement. Here is a working example:
(function() {
angular
.module("app", []);
angular
.module("app")
.controller("AppController", AppController);
AppController.$inject = ["$scope"];
function AppController($scope) {
var vm = this;
vm.selectedYear = "";
vm.years = [];
loadData();
function loadData() {
var year = new Date().getFullYear();
var range = [];
range.push(year);
for (var i = 1; i < 20; i++) {
range.push(year + i);
}
vm.years = range;
}
}
})();
<!DOCTYPE html>
<html ng-app="app">
<head>
<script data-require="angular.js#1.6.1" data-semver="1.6.1" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js"></script>
<script src="app.js"></script>
</head>
<body ng-controller="AppController as vm">
<div>
<select ng-model="vm.selectedYear"ng-options="year for year in vm.years">
</select>
</div>
</body>
</html>

Angularjs, the controller can't read from the factory?

I have a services.js file where I placed my facotry:
.factory('dataShare',function($rootScope){
var service = {};
service.data = false;
service.sendData = function(data){
this.data = data;
$rootScope.$broadcast('data_shared');
};
service.getData = function(){
return this.data;
console.log('this.data')
};
return service;
})
And my controllers, in the controller.js file look like this:
.controller('recommendedJobsCtrl', function($q,$scope, $ionicSideMenuDelegate,$window,$http,dataShare) {
$scope.text='hey';
$scope.post=function(){
dataShare.sendData($scope.text);
console.log('here')
}
})
.controller('jobPostCtrl', function($scope,$ionicSideMenuDelegate,dataShare) {
$scope.text = '';
$scope.$on('data_shared',function(){
var text = dataShare.getData();
$scope.text = text;
console.log(text)
});
})
When I did console.log, i realized that the service.getData isn't wokring, i.e the receiving controller (jobPostCtrl) isn't getting anything.
How can i fix this?
Here is example that works.
<!doctype html>
<html>
<head>
<title>AngularJS</title>
<script src="angular.min.js"></script>
<script type="text/javascript">
var sampleApp = angular.module('sampleApp', []);
sampleApp.factory('dataShare',function($rootScope){
var service = {};
service.data = false;
service.sendData = function(data){
this.data = data;
$rootScope.$broadcast('data_shared');
};
service.getData = function(){
return this.data;
console.log('this.data')
};
return service;
});
sampleApp.controller('recommendedJobsCtrl', function($q,$scope,$window,$http,dataShare) {
$scope.text='hello world!';
$scope.post=function(){
dataShare.sendData($scope.text);
console.log('sent: ' + $scope.text);
}
});
sampleApp.controller('jobPostCtrl', function($scope,dataShare) {
$scope.text = '';
$scope.$on('data_shared',function(){
var text = dataShare.getData();
$scope.text = text;
console.log('received: ' + $scope.text);
});
});
</script>
</head>
<body ng-app="sampleApp">
<div ng-controller="recommendedJobsCtrl">
<input type="text" ng-model="text" />
<button ng-click="post()">Send</button>
</div>
<div ng-controller="jobPostCtrl">
<p>{{text}}</p>
</div>
</body>
</html>
Use
$rootScope.$broadcast('data_shared',this.data);
and $scope.$on('data_shared',function(event,data){
$scope.$scope.text=data;
});

changing an image into another image and then resets back to default image when another different image is clicked in angularjs

How will I change my image and the roll it back to default image when I click another image using angularjs? I am new to angular and here is my code
<div ng-controller="SwapControl">
<img ng-click="myData.swapHere()" ng-src="{{myData.images.current}}" alt="image to be swapped">
</div>
<script>
var myApp = angular.module("swap", []);
myApp.controller('SwapControl', function($scope) {
$scope.myData = {};
$scope.myData.images = {
initialImage: "http://creativecurio.com/wp-content/uploads/2007/vm-logo-sm-1.gif",
finalImage: "http://www.polyvore.com/cgi/img-thing?.out=jpg&size=l&tid=1247787",
current : "http://creativecurio.com/wp-content/uploads/2007/vm-logo-sm-1.gif"
};
$scope.myData.swapHere = function() {
if($scope.myData.images.current === $scope.myData.images.finalImage)
$scope.myData.images.current = $scope.myData.images.initialImage
else if($scope.myData.images.current === $scope.myData.images.initialImage)
$scope.myData.images.current = $scope.myData.images.finalImage
};
$scope.myData1 = {};
$scope.myData1.images = {
initialImage: "http://www.wired.com/wp-content/uploads/2015/09/google-logo.jpg",
finalImage: "http://www.polyvore.com/cgi/img-thing?.out=jpg&size=l&tid=1247787",
current : "http://www.wired.com/wp-content/uploads/2015/09/google-logo.jpg"
};
$scope.myData1.swapHere = function() {
if($scope.myData1.images.current === $scope.myData1.images.finalImage)
$scope.myData1.images.current = $scope.myData1.images.initialImage
else if($scope.myData1.images.current === $scope.myData1.images.initialImage)
$scope.myData1.images.current = $scope.myData1.images.finalImage
};
});
</script>
<div ng-controller="SwapControl">
<img ng-click="myData1.swapHere()" ng-src="{{myData1.images.current}}" alt="image to be swapped">
</div>
</body>
here is the plunker link: http://plnkr.co/edit/zPxqPjB4M1sis16SF7Jn?p=preview
First of all, your images are controlled by two diffrent instances of the same controller. Then, every image is in diffrent scope so first image can't be controlled by the second one. To change this, move your ng-controller attribute to <body> element.
Now you can change ng-click attribute for second image and use the function that changes first image. Below is a working code:
<body ng-app="swap" ng-cpontroller="SwapControl">
<div>
<img ng-click="myData.swapHere()" ng-src="{{myData.images.current}}" alt="image to be swapped">
</div>
<script>
var myApp = angular.module("swap", []);
myApp.controller('SwapControl', function($scope) {
$scope.myData = {};
$scope.myData.images = {
initialImage: "http://creativecurio.com/wp-content/uploads/2007/vm-logo-sm-1.gif",
finalImage: "http://www.polyvore.com/cgi/img-thing?.out=jpg&size=l&tid=1247787",
current : "http://creativecurio.com/wp-content/uploads/2007/vm-logo-sm-1.gif"
};
$scope.myData.swapHere = function() {
$scope.myData1.images.current = $scope.myData1.images.initialImage
if($scope.myData.images.current === $scope.myData.images.finalImage)
$scope.myData.images.current = $scope.myData.images.initialImage
else if($scope.myData.images.current === $scope.myData.images.initialImage)
$scope.myData.images.current = $scope.myData.images.finalImage
};
$scope.myData1 = {};
$scope.myData1.images = {
initialImage: "http://www.wired.com/wp-content/uploads/2015/09/google-logo.jpg",
finalImage: "http://www.polyvore.com/cgi/img-thing?.out=jpg&size=l&tid=1247787",
current : "http://www.wired.com/wp-content/uploads/2015/09/google-logo.jpg"
};
$scope.myData1.swapHere = function() {
$scope.myData.images.current = $scope.myData.images.initialImage
if($scope.myData1.images.current === $scope.myData1.images.finalImage)
$scope.myData1.images.current = $scope.myData1.images.initialImage
else if($scope.myData1.images.current === $scope.myData1.images.initialImage)
$scope.myData1.images.current = $scope.myData1.images.finalImage
};
});
</script>
<div>
<img ng-click="myData1.swapHere()" ng-src="{{myData1.images.current}}" alt="image to be swapped">
</div>
</body>

Angular Js and Partition

I want to show the data in the form of row and column . I am having list of data in the form of array . I am using onsenui
Here is the code
<link rel="stylesheet" href="lib/onsen/css/onsenui.css">
<link rel="stylesheet" href="lib/onsen/css/onsen-css-components.css">
<script src="lib/onsen/js/angular/angular.js"></script>
<script src="lib/onsen/js/onsenui.js"></script>
<script>
ons.bootstrap();
function MyCtrl($scope)
{
$scope.inside ="asd";
$scope.names = [
{name:'Mac',path:'http://www.hdicon.com/wp-content/uploads/2010/07/McDonalds_golden_arch.png'},
{name:'KFC',path:'http://facebookazine.com/wp-content/uploads/2012/06/KFC_icon.jpg'},
{name:'Karlsruhe',path:'http://media-cache-ak0.pinimg.com/736x/8b/55/44/8b55442ccb4d3f3ac514a1dceaa3ea43.jpg'}
];
}
</script>
Here is the html code
<div ng-controller="MyCtrl" >
<ons-row ng-repeat ="x in names| partition:2" class="center">
<ons-col>
<img src ="{{x.path}}" width="100px"/>
</ons-col>
<ons-col>
<h4>{{x.name}}</h4>
</ons-col>
</ons-row>
</div>
When i open the page i am getting this error
Error: [$injector:unpr] Unknown provider: partitionFilterProvider <- partitionFilter
http://errors.angularjs.org/1.2.10/$injector/unpr?p0=partitionFilterProvider%20%3C-%20partitionFilter
I searched this error on Internet but i didnt get any solution to this
Check below code (Filter partition code is implemented in my project which was taken from blog)
var app = angular.module('MyApp', [])
.controller('MyCtrl', function ($scope) {
$scope.inside ="asd";
$scope.names = [
{name:'Mac',path:'http://www.hdicon.com/wp-content/uploads/2010/07/McDonalds_golden_arch.png'},
{name:'KFC',path:'http://facebookazine.com/wp-content/uploads/2012/06/KFC_icon.jpg'},
{name:'Karlsruhe',path:'http://media-cache- ak0.pinimg.com/736x/8b/55/44/8b55442ccb4d3f3ac514a1dceaa3ea43.jpg'}
];
}
.filter('partition', function () {
var cache = {};
var filter = function (arr, size) {
if (!arr) { return; }
var newArr = [];
for (var i = 0; i < arr.length; i += size) {
newArr.push(arr.slice(i, i + size));
}
var arrString = JSON.stringify(arr);
var fromCache = cache[arrString + size];
if (JSON.stringify(fromCache) === JSON.stringify(newArr)) {
return fromCache;
}
cache[arrString + size] = newArr;
return newArr;
};
return filter;
})

Resources