Please find my code sample below.
Code in Model(HTML Code)
<select ng-model="drpDown1" ng-options="option.Title as option.Title for option in drpDown1Options">
<option value="">Select a value..</option>
</select>
<br/>
<select ng-model="drpDown1" ng-options="option.Title as option.Title for option in drpDown2Options">
<option value="">Select a value..</option>
</select>
For binding values to the above dropDown I am using REST api in SharePoint 2013 App model
And Also, I have lot many such dropdowns..I have to use same code for every dropdown except there will be a change in listname, variable used to bind the values.
So I want to use a service and placed the below code in a service called myService.js
I am using the below Code.
this.bindDropdown = function (listname,dropDownOptionsVar,$rootScope) {
debugger;
var arr = [];
var listName = listname;
var scriptbase = hostweburl + "/_layouts/15/";
var requestUrl = appweburl + "/_api/SP.AppContextSite(@target)/Web/Lists/getbytitle('" + listName + "')/items?&@target='" + hostweburl + "'";
var executor = new SP.RequestExecutor(appweburl);
executor.executeAsync({
url: requestUrl,
method: "GET",
headers: { "Accept": "application/json;odata=verbose" },
success: function (data) {
var responseData = JSON.parse(data.body);
for (var i = 0; i < responseData.d.results.length; i++) {
var Title = responseData.d.results[i].Title;
var Id = responseData.d.results[i].Id;
arr1.push({
ID: Id,
Title: Title
});
}
$rootScope.dropDownOptionsVar = arr;
$scope.$apply();
},
error: function (data, errorCode, errorMessage) {
alert(errorMessage);
}
});
}
I am calling above function of myServicve.js from myController.js as below.
myController.js Code
$rootScope.drpDown1Options ='';
$rootScope.drpDown2Options = '';
mySerivce.bindDropdown("List1",$rootScope.drpDown1Options,$rootScope);
mySerivce.bindDropdown("List2",$rootScope.drpDown2Options,$rootScope);
So the problem here is Its not working fine.. The parameter $rootScope.drpDown1Options is not passed to the function in mySerivce.js
Its working fine only when I am changing the below statement
$rootScope.dropDownOptionsVar = arr;
into
$rootScope.drpDown1Options = arr;
$rootScope.drpDown2Options = arr; individually in individual functions ( By creating one function for binding one dropdwon)