Angularjs应用程序范围变量对视图不可用

时间:2014-03-11 14:33:57

标签: angularjs angularjs-scope

在下面的angularjs代码中,为什么IsLoggedIn和Username对视图不可用。不确定是什么错,任何帮助都将受到高度赞赏:

'use strict';

myApp.controller('masterPageController',

    function masterPageController($scope, loginService, stateService) {

        $scope.State = stateService.State;

});

myApp.factory('stateService', function () {

    // todo: fetch the state from server when the service is initialised
     var state = {};

    return {
        State: state,
    };
});

myApp.factory('loginService', function ($http, $q, stateService) {


    return {

        Login: function (logindetails) {
            var deferred = $q.defer();

            $http({ method: 'POST', url: '/api/Login/Login', data: JSON.stringify(logindetails), headers: { 'Content-Type': 'application/json' } }).
               success(function (data, status, headers, config) {

                   stateService.State = data.State;
                   deferred.resolve(data);

               }).
               error(function (data, status, headers, config) {
                   deferred.reject(status);
               });

            return deferred.promise;
        }
    };
});

<div ng-controller="masterPageController">
    <div>
        <div>
            <div>
                <a href="/logout" ng-show="State.IsLoggedIn" >{{ State.Username }}</a>

            </div>

        </div>
    </div>
</div>
编辑:当我在loginService中设置stateService.State属性时,它不起作用

1 个答案:

答案 0 :(得分:-1)

从&#34;工厂&#34;更改您构建服务的方式到&#34;服务&#34;并定义&#34; state&#34;在此,不要返回任何东西。

myApp.service(&#39; stateService&#39;,function(){

CREATE FUNCTION [dbo].[XC_CALCULATE_DUE_DATE] (
    @INVOICEDATE DATETIME
    ,@TENANTID BIGINT
    ,@PAYMENTTERMID BIGINT
    )
RETURNS DATETIME
AS
BEGIN
    DECLARE @DAYOFMONTH BIGINT
    DECLARE @DAYOFNEXTMONTH BIGINT
    DECLARE @PAYMENTTYPES BIGINT
    DECLARE @NOOFDAYS BIGINT

    SELECT @PAYMENTTYPES = PAYMENT_TYPES
        ,@NOOFDAYS = NUMBER_OF_DAYS
        ,@DAYOFMONTH = DAY_OF_MONTH
        ,@DAYOFNEXTMONTH = DAYS_AFTER_DUE_DAY
    FROM XC_PAYMENT_TERMS_MASTER
    WHERE TENANT_ID = @TENANTID
        AND PAYMENT_TERM_ID = @PAYMENTTERMID

    DECLARE @DIFFERENCE BIGINT = ISNULL(@DAYOFMONTH, 0) - ISNULL(@DAYOFNEXTMONTH, 0)
    DECLARE @DUEDATE DATETIME
    DECLARE @ACTUALDUEDATE BIGINT
    DECLARE @CALCULATEDDATE DATETIME

    IF @PAYMENTTYPES = 1
    BEGIN
        SET @DUEDATE = DATEADD(dd, @NOOFDAYS, @INVOICEDATE)
    END
    ELSE IF @PAYMENTTYPES = 2
    BEGIN
        DECLARE @LEAPYEAR TINYINT = IIF(DATEPART(dd, (
                        EOMONTH(CONCAT (
                                DATEPART(yyyy, @INVOICEDATE)
                                ,'0201'
                                ))
                        )) = 29, 1, 0)

        IF @DIFFERENCE <= 0
        BEGIN
            DECLARE @STARTINGDATE DATETIME
            DECLARE @NEWDATE DATETIME
            DECLARE @NEXTMONTH DATETIME

            IF 1 = @LEAPYEAR
            BEGIN
                SET @STARTINGDATE = DATEADD(month, DATEDIFF(month, 0, @INVOICEDATE), 0)
                SET @NEXTMONTH = DATEADD(MONTH, 1, @STARTINGDATE)

                IF 1 = DATEPART(MONTH, @INVOICEDATE)
                BEGIN
                    IF @DAYOFMONTH = 30
                    BEGIN
                        SET @DUEDATE = DATEADD(month, DATEDIFF(month, 0, DATEADD(MONTH, 2, @INVOICEDATE)), 0)
                    END
                    ELSE IF @DAYOFMONTH = 31
                    BEGIN
                        SET @DUEDATE = DATEADD(DAY, 1, DATEADD(month, DATEDIFF(month, 0, DATEADD(MONTH, 2, @INVOICEDATE)), 0))
                    END
                    ELSE
                    BEGIN
                        SET @DUEDATE = DATEADD(DAY, @DAYOFMONTH - 1, @NEXTMONTH)
                    END
                END
                ELSE
                BEGIN
                    SET @DUEDATE = DATEADD(DAY, @DAYOFMONTH - 1, @NEXTMONTH)
                END
            END
            ELSE
            BEGIN
                SET @STARTINGDATE = DATEADD(month, DATEDIFF(month, 0, @INVOICEDATE), 0)
                SET @NEXTMONTH = DATEADD(MONTH, 1, @STARTINGDATE)

                IF 1 = DATEPART(MONTH, @INVOICEDATE)
                BEGIN
                    IF @DAYOFMONTH = 29
                    BEGIN
                        SET @DUEDATE = DATEADD(month, DATEDIFF(month, 0, DATEADD(MONTH, 2, @INVOICEDATE)), 0)
                    END
                    ELSE IF @DAYOFMONTH = 30
                    BEGIN
                        SET @DUEDATE = DATEADD(DAY, 1, DATEADD(month, DATEDIFF(month, 0, DATEADD(MONTH, 2, @INVOICEDATE)), 0))
                    END
                    ELSE IF @DAYOFMONTH = 31
                    BEGIN
                        SET @DUEDATE = DATEADD(DAY, 2, DATEADD(month, DATEDIFF(month, 0, DATEADD(MONTH, 2, @INVOICEDATE)), 0))
                    END
                    ELSE
                    BEGIN
                        SET @DUEDATE = DATEADD(DAY, @DAYOFMONTH - 1, @NEXTMONTH)
                    END
                END
                ELSE
                BEGIN
                    SET @DUEDATE = DATEADD(DAY, @DAYOFMONTH - 1, @NEXTMONTH)
                END
            END
        END
        ELSE
        BEGIN
            DECLARE @SDATES DATETIME = DATEADD(month, DATEDIFF(month, 0, @INVOICEDATE), 0)
            DECLARE @NMONTH DATETIME = DATEADD(MONTH, 1, @SDATES)

            IF DATEPART(DD, @INVOICEDATE) > @DIFFERENCE
            BEGIN
                IF @DAYOFMONTH > 30
                    AND DATEPART(DD, @INVOICEDATE) <> @DIFFERENCE
                BEGIN
                    SET @DUEDATE = DATEADD(s, - 1, DATEADD(mm, DATEDIFF(m, 0, @NMONTH) + 1, 0))
                END
                ELSE
                BEGIN
                    SET @DUEDATE = DATEADD(DAY, @DAYOFMONTH - 1, @NMONTH)
                END
            END
            ELSE
            BEGIN
                IF @DAYOFMONTH > 30
                    AND DATEPART(DD, @INVOICEDATE) <> @DIFFERENCE
                BEGIN
                    SET @DUEDATE = DATEADD(s, - 1, DATEADD(mm, DATEDIFF(m, 0, @NMONTH) + 1, 0))
                END
                ELSE IF @DAYOFMONTH > 30
                    AND DATEPART(DD, @INVOICEDATE) = @DIFFERENCE
                BEGIN
                    SET @DUEDATE = DATEADD(s, - 1, DATEADD(mm, DATEDIFF(m, 0, @SDATES) + 1, 0))
                END
                ELSE
                BEGIN
                    SET @DUEDATE = DATEADD(DAY, @DAYOFMONTH - 1, @SDATES)
                END
            END

            IF 1 = @LEAPYEAR
            BEGIN
                IF 1 = DATEPART(MONTH, @INVOICEDATE)
                    AND DATEPART(DD, @INVOICEDATE) <> @DIFFERENCE
                BEGIN
                    IF @DAYOFMONTH > 29
                    BEGIN
                        SET @DUEDATE = DATEADD(s, - 1, DATEADD(mm, DATEDIFF(m, 0, @NMONTH) + 1, 0))
                    END
                END
            END
            ELSE
            BEGIN
                IF 1 = DATEPART(MONTH, @INVOICEDATE)
                    AND DATEPART(DD, @INVOICEDATE) <> @DIFFERENCE
                BEGIN
                    IF @DAYOFMONTH >= 29
                    BEGIN
                        SET @DUEDATE = DATEADD(s, - 1, DATEADD(mm, DATEDIFF(m, 0, @NMONTH) + 1, 0))
                    END
                END
            END
        END
    END

    RETURN @DUEDATE
END

});

http://codepen.io/rotempe4/pen/KNMpzZ