Angular Service返回数据但未在HTML页面中显示

时间:2017-04-09 14:13:05

标签: angularjs angularjs-service

我是AngularJS的新手,我正在练习定制服务。以下服务正在返回数据,我使用警告框进行了检查,但数据未显示在页面上。

我的HTML:

app = angular.module("app1", [])
.service('NumberService', function () {
  this.square= function(a){return a*a; };
  this.cube = function(a){return  a * a * a; };
})

.controller('ServiceController', ['$scope','NumberService',

function ($scope, NumberService) {

  $scope.getData = function () {
    //  alert('Button clicked' );

    var n = $scope.a;        
    $scope.Square = NumberService.square(n);
    alert($scope.Square);
    $scope.Cube = NumberService.cube(n);
  }
 }
]);

我的app.js文件:

angular.module("app",[])
.controller("ctrl",function($scope, $compile, $timeout){
        $scope.name = 'blah blah';
        $scope.message = 'Good day';
        var returnedTemplate = '<div><h3 class="md-title">{{name}}</h3><p class="md-title">{{message}}</p></div>';
        var element = $compile(returnedTemplate)($scope);

        $timeout(function() {
            var confirmDialogMessage = element.html(); // needed time
            console.log(confirmDialogMessage); // here I got the html with values in, but still in a string

       }, 300);
        
})

1 个答案:

答案 0 :(得分:3)

您需要在此处将var start = new Date("2017-04-09T21:00:00"); start.setTime(start.getTime() + start.getTimezoneOffset()*60*1000); console.log(start);替换为ng-model。 ng-modal用于双向绑定。因此它在这里不起作用。

ng-bind

Square: <span ng-model="Square"></span>  <br />
    Cube: <span ng-model="Cube"></span>