In my project, I use ngRoute to load the edit.php view into my index ng-view
I dont know why the ngResource result not binding to my ng-model, here is my code:
Javascript:
var app = angular.module('smodule', ['ngRoute','ngResource']);
/* Factory */
app.factory('Stocks', function($resource){
return $resource('<?=$this->module_url;?>/stocks/:stockId', {stockId:'@stock_id'}, {
query: {method:'GET', isArray:true},
get: {method:'GET',isArray:true, params:{stock_id: '@stock_id'}},
update: {method:'PUT'}
});
});
/* Router */
app.config(function($routeProvider) {
$routeProvider
.when('/stock',{
templateUrl: 'edit.php',
controller: 'stockDetailController'
})
.when('/stock/:stockId', {
templateUrl: 'edit.php',
controller: 'stockDetailController'
});
});
/*Controller*/
app.controller('stockDetailController', function($scope, Stocks, $routeParams){
console.log($routeParams.stockId);
if ($routeParams.stockId){
/* Update stock data */
Stocks.get({stockId:$routeParams.stockId}, function(stock){
$scope.stock = stock;
console.log(stock);
});
}
});
/* Bootstrap the module as ng-module */
angular.bootstrap(document, ['smodule']);
edit.php:
<div class="columns medium-8 large-8 ">
<label>Stock name
<input type="text" name="stock_name" ng-model="stock.name" />
</label>
</div>
Backend return the following json
[
{
id: "1",
name: "My stock name"
}
]
I used chrome debugger to check if any result returned from the ngResource and it shown the following:
[Resource, $promise: Object, $resolved: true]
0: id: "1",
name: "My stock name"
$promise: Object
$resolved: truelength: 1
Anyone can help? Thank you.