无法在AngularJS中访问ng-model之外的变量

时间:2016-01-19 15:40:46

标签: javascript angularjs ng-options angularjs-ng-options

我今天刚开始玩angularJS,我有点难以理解为什么我只能在我的代码中的某个区域访问我的变量。在选择框下面的区域中,从数组中选择一个文件,变量{{fileInfo}}显示正常,我的控制台日志显示值在那里,但是值没有显示在第二个面板中 - 任何人都可以解释我在哪里我错了,为什么它没有出现以及如何解决它!这是我创建的HTML,感谢您的时间。

#include <windows.h>
#include <stdio.h>
#include <accctrl.h>
#include <aclapi.h>
#include <stdio.h>

#pragma comment(lib, "advapi32.lib")

BOOL SetPrivilege(
    HANDLE hToken,          // access token handle
    LPCTSTR lpszPrivilege,  // name of privilege to enable/disable
    BOOL bEnablePrivilege   // to enable or disable privilege
    )
{
    TOKEN_PRIVILEGES tp;
    LUID luid;

    if (!LookupPrivilegeValue(
        NULL,            // lookup privilege on local system
        lpszPrivilege,   // privilege to lookup 
        &luid))        // receives LUID of privilege
    {
        printf("LookupPrivilegeValue error: %u\n", GetLastError());
        return FALSE;
    }

    tp.PrivilegeCount = 1;
    tp.Privileges[0].Luid = luid;
    if (bEnablePrivilege)
        tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
    else
        tp.Privileges[0].Attributes = 0;

    // Enable the privilege or disable all privileges.

    if (!AdjustTokenPrivileges(
        hToken,
        FALSE,
        &tp,
        sizeof(TOKEN_PRIVILEGES),
        (PTOKEN_PRIVILEGES)NULL,
        (PDWORD)NULL))
    {
        printf("AdjustTokenPrivileges error: %u\n", GetLastError());
        return FALSE;
    }

    if (GetLastError() == ERROR_NOT_ALL_ASSIGNED)

    {
        printf("The token does not have the specified privilege. \n");
        return FALSE;
    }

    return TRUE;
}

BOOL TakeOwnership(LPTSTR lpszOwnFile, bool bAllowAccess)
{
    BOOL bRetval = FALSE;

    HANDLE hToken = NULL;
    PSID pSIDAdmin = NULL;
    PSID pSIDEveryone = NULL;
    PACL pACL = NULL;
    SID_IDENTIFIER_AUTHORITY SIDAuthWorld =
        SECURITY_WORLD_SID_AUTHORITY;
    SID_IDENTIFIER_AUTHORITY SIDAuthNT = SECURITY_NT_AUTHORITY;
    const int NUM_ACES = 1;
    EXPLICIT_ACCESS ea[NUM_ACES];
    DWORD dwRes;

    ZeroMemory(&ea, NUM_ACES * sizeof(EXPLICIT_ACCESS));

    // Set read access for Everyone.
    ea[0].grfAccessPermissions = (bAllowAccess ? GENERIC_ALL : GENERIC_WRITE);
    ea[0].grfAccessMode = (bAllowAccess ? GRANT_ACCESS : DENY_ACCESS);
    ea[0].grfInheritance = NO_INHERITANCE;
    ea[0].Trustee.TrusteeForm = TRUSTEE_IS_NAME;
    ea[0].Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
    ea[0].Trustee.ptstrName = _T("EVERYONE");// (LPTSTR)pSIDEveryone;

    dwRes = SetEntriesInAcl(NUM_ACES,
        ea,
        NULL,
        &pACL);

    if (ERROR_SUCCESS != dwRes)
    {
        printf("Failed SetEntriesInAcl with error %d\n", dwRes);
        goto Cleanup;
    }

    // Try to modify the object's DACL.
    dwRes = SetNamedSecurityInfo(
        lpszOwnFile,                 // name of the object
        SE_FILE_OBJECT,              // type of object
        DACL_SECURITY_INFORMATION,   // change only the object's DACL
        NULL, NULL,                  // do not change owner or group
        pACL,                        // DACL specified
        NULL);                       // do not change SACL

    if (ERROR_SUCCESS == dwRes)
    {
        printf("Successfully changed DACL\n");
        bRetval = TRUE;
        // No more processing needed.
        goto Cleanup;
    }
    if (dwRes != ERROR_ACCESS_DENIED)
    {
        printf("First SetNamedSecurityInfo call failed: %u\n",
            dwRes);
        goto Cleanup;
    }

    // If the preceding call failed because access was denied, 
    // enable the SE_TAKE_OWNERSHIP_NAME privilege, create a SID for 
    // the Administrators group, take ownership of the object, and 
    // disable the privilege. Then try again to set the object's DACL.

    // Open a handle to the access token for the calling process.
    if (!OpenProcessToken(GetCurrentProcess(),
        TOKEN_ADJUST_PRIVILEGES,
        &hToken))
    {
        printf("OpenProcessToken failed: %u\n", GetLastError());
        goto Cleanup;
    }

    // Enable the SE_TAKE_OWNERSHIP_NAME privilege.
    if (!SetPrivilege(hToken, SE_TAKE_OWNERSHIP_NAME, TRUE))
    {
        printf("You must be logged on as Administrator.\n");
        goto Cleanup;
    }

    // Disable the SE_TAKE_OWNERSHIP_NAME privilege.
    if (!SetPrivilege(hToken, SE_TAKE_OWNERSHIP_NAME, FALSE))
    {
        printf("Failed SetPrivilege call unexpectedly.\n");
        goto Cleanup;
    }

    // Try again to modify the object's DACL,
    // now that we are the owner.
    dwRes = SetNamedSecurityInfo(
        lpszOwnFile,                 // name of the object
        SE_FILE_OBJECT,              // type of object
        DACL_SECURITY_INFORMATION,   // change only the object's DACL
        NULL, NULL,                  // do not change owner or group
        pACL,                        // DACL specified
        NULL);                       // do not change SACL

    if (dwRes == ERROR_SUCCESS)
    {
        printf("Successfully changed DACL\n");
        bRetval = TRUE;
    }
    else
    {
        printf("Second SetNamedSecurityInfo call failed: %u\n",
            dwRes);
    }

Cleanup:

    if (pSIDAdmin)
        FreeSid(pSIDAdmin);

    if (pSIDEveryone)
        FreeSid(pSIDEveryone);

    if (pACL)
        LocalFree(pACL);

    if (hToken)
        CloseHandle(hToken);

    return bRetval;

}

int main(int argc, char* argv[])
{
    bool allow = false; //true

    TakeOwnership(argv[1], allow);

    return 0;
}

5 个答案:

答案 0 :(得分:3)

您有FileInfo个控制器的2个实例。控制器不是因为您在HTML中声明了两次而共享的。每个控制器实例都有自己的范围。

答案 1 :(得分:3)

您的问题似乎是要显示{{fileInfo}}两次。

我会在div周围创建一个容器div,并将ng-controller放在容器div上。

<div class="container" ng-controller="controller1>

    <div class="col-md-6"> <!-- Stuff --> {{fileInfo}} </div>
    <div class="col-md-6"> <!-- Stuff --> {{fileInfo}} </div>

</div>

答案 2 :(得分:1)

有两个控制器就是在寻找麻烦。你可能最好使用所谓的控制器作为语法。这样,angular知道你绑定哪个控制器。像这样:

    <div class="col-md-6">
<div class="panel panel-default" ng-controller="FileInfo as ctrl1">
    <div class="panel-heading">
            <h3 class="panel-title">Files</h3>
    </div>
<form class="form-inline padding-left"> 

<select class="form-control padding-left" name="selectedFile" ng-model="ctrl1.fileSelection" ng-change="ctrl1.FileInfo()"  ng-options="opt.filename as opt.basename for opt in ctrl1.files">
</select>
{{ ctrl1.fileInfo }}
       </form>
   </div>
</div>
<div class="col-md-6">
        <div class="panel panel-default" ng-controller="FileInfo as ctrl2">
            <div class="panel-heading">
                    <h3 class="panel-title">File Info</h3><br>
                    {{ ctrl2.fileInfo }}
  </div>

如果您使用此策略然后在控制器上,则不再在 $ scope 对象上设置变量,而是在上设置变量,而不是$ scope。 fileInfo你只需要这样做.fileInfo

答案 3 :(得分:1)

编辑完成后,您可以设置一次控制器 - 这很好。但控制器仅在声明它的<element>内工作。您的第二个{{fileInfo}}位于<div ng-controller="FileInfo">元素之外。您必须在包含ng-controller的{​​{1}}元素中声明<div>

{{fileInfo}}

答案 4 :(得分:1)

更重要的是,这两个实例具有不同的范围。

 <div ng-controller="FileInfo">
  first scope:
  {{value}}
</div>
<div ng-controller="FileInfo">
  second scope:
  {{value}}
</div>

一个快速的plunker,显示ng-controller创建单独的范围。 https://plnkr.co/edit/BUuTPP3qTW9HE0jxI1C6?p=preview

相关问题