【问题标题】:Setting ng-bind value dynamically through controller, not using an expression通过控制器动态设置 ng-bind 值,而不是使用表达式
【发布时间】:2018-07-25 09:36:19
【问题描述】:
<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 而不是分配一个常量字符串。 Angular 可以吗?

【问题讨论】:

    标签: angularjs angularjs-ng-model angular-controller ng-bind


    【解决方案1】:

    是的,你可以使用工厂来做到这一点

    //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
      })
    
    }])

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-19
      • 1970-01-01
      • 2013-09-06
      • 2018-02-18
      • 1970-01-01
      • 1970-01-01
      • 2017-12-18
      • 2015-10-27
      相关资源
      最近更新 更多