I'm trying to populate an html list using AngularJs in the following way. I want to create a service that takes an html partial, populates it's attributes in the constructor and returns it. Below is my example for this.
js:
var app = angular.module('plunker', []);
app.controller('MainCtrl',['$scope','listElemService', function($scope,listElemService) {
$scope.listElemArray = [];
var testParam1 = {};
var tester = listElemService(testParam1);
$scope.listElemArray.push(new listElemService(testParam1));
var testParam2 = {};
testParam2.url = "http://www.yahoo.com";
testParam2.imgSrc = "https://pbs.twimg.com/profile_images/378800000416430559/3fab1a175e2a87010f23435e0aea0f61_400x400.jpeg";
$scope.listElemArray.push(new listElemService(testParam2));
}]).factory('listElemService',function(){
var ListElemConstructor = function(params) {
this.initialize = function() {
this.url = params.url;
this.imgSrc = params.imgSrc;
/*
want to be able to construct list element below
from listElemView.html based on params.url and params.imgSrc
*/
this.html = "Html Placeholder";
};
this.initialize();
};
return ListElemConstructor;
});
main html:
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<link rel="stylesheet" href="style.css" />
<script data-require="[email protected]" src="https://code.angularjs.org/1.4.0-rc.1/angular.js" data-semver="1.4.0-rc.1"></script>
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
<ul>
<li ng-repeat="x in listElemArray">
{{x.html}}
</li>
</ul>
</body>
</html>
Html Partial that I'd like to 'bring in' to my service and populate:
<li>
<img src=""/>
<a href=""></a>
</li>
{{x.html}}if you can simply<img src="{{x.imgSrc}}"/> <a href="x.url"></a>?