If there's something out there I'd love to see some documentation on something like this.
I'm trying to add a value from the query string to the scope of my controller and use that value in another function down the page.
This is what I've tried:
var id = Number(getQueryStringValue("id"));
if (!!id) {
$('[ng-controller="appControl"]').scope().id = id;
}
To set the value in scope and:
<p>
id: {{id}}
</p>
down the page to print out the id. If I do something like this:
var x = $('[ng-controller="appControl"]').scope();
I can see that id has indeed been added to the scope but then the only output I get is id:
. I can also see that the id is in fact a number and not a string or some other value that would not print.
--Edit--
Since it seems that this is not the best way to do this, I've looked into factories, the following is my code (which still doesn't work for some reason) and is contained in a separate file:
var app = angular.module('app', []);
app.controller('appctrl', function ($scope) {
$scope.dataArr= [
{'name': 'Yo'},
{'name': 'Dawg'}
];
});
app.factory('Data', function () {
var idValue = 0;
return {
setId: function (id) {
idValue = id;
},
getId: function () {
return idValue;
}
}
});
This is the script on the page in question:
<script type="text/javascript">
$(function () {
var id = Number(getQueryStringValue("id"));
if (!!id) {
app.controller('appctrl', function($scope, Data) {
$scope.id = Data.setId(id);
});
}
});
</script>
<div ng-app="app">
<div ng-controller="appctrl">
{{id}}
</div>
</div>