<div ng-app =“App” ng-controller = “ctrl”>
<input ng-model = “firstName”>
<p ng-bind = “showFirstName”></p>
<script>
var app=(‘App’,[]);
app.controller(‘ctrl’,function($scope)
{
//This Line
$scope.showFirstName = $scope.firstName;
});
</script>
我想动态设置showFirstName而不是分配常量String。 Angular可以吗?
答案 0 :(得分:0)
是的,您可以使用工厂进行
//Create App
var app = angular.module('myApp',[]);
//Create a factory
app.factory('DbService', function($http){
return{
fetchData : function(parameters){
return $http.post('/url-to-fetch-data',parameters).then(function (response) {
return response.data;
});
}
}
});
//inject factory to controller
app.controller('mycontroller', ['$scope', 'DbService', function($scope, DbService){
$scope.name = '';
//parameters is a javascript object here you can pass the parameters from client if you want
var parameters = {id:'125', token:"sd13ds6dsds454dsd4"};
DbService.fetchData(parameters).then(function(data){
$scope.name = data.name
})
}])