在HTML中获取$ scope变量的值

时间:2015-07-18 12:05:43

标签: jquery angularjs

我是Angular的新手。 我遇到了问题,就像我在Controller JS中创建变量但无法在HTML部分中获取。

<html ng-app='Demo'>    
<body>
            <h2>Select Your Symbol</h2><br><br>
            <div class='selection' ng-controller='MasterController' ng-init="selectedSymbol = ''" >
                <div class=" col-md-3"></div>
                <div  ng-repeat="symbol in symbols" ng-model="selectedSymbol">
                    <a class='col-xs-1 box' id={{symbol.definition}} ng-click="remove(symbol); user= symbol.definition" ><big class='fixed'>{{symbol.definition}}</big></a>
                </div>
            </div><br>
            {{selectedSymbol}}
        </body>
</html>

JS部分

(function( ng) {

    "use strict";

    // Define our AngularJS application module.
    window.demo = ng.module( "Demo", [] );

})( angular );
(function( ng, app ) {

    "use strict";

    app.controller(
        "MasterController",
        function( $scope ) {


            // -- Define Controller Methods. ---------------- //

                $scope.symbols = [
            {
                definition: 'X',
            },
            {
                definition: 'O',
            }
        ];
        $scope.remove = function(symbol) {
         if(symbol.definition == 'X' && this.symbols.length >1)
         {
            this.symbols.splice(1);
            $scope.selectedSymbol = 'X';
            alert($scope.selectedSymbol);
         }
         else if (symbol.definition == 'O' && this.symbols.length >1)
         {
            this.symbols.splice(0,1);
             $scope.selectedSymbol = 'O';
             alert($scope.selectedSymbol);
         } 

        };
        }
    );

})( angular, demo );

现在,我可以在$scope.selectedSymbol中看到Alert,但html无法看到 Sub load_txt_code(ByVal nkala As ComboBox, ByVal ckala As ComboBox, ByVal ghyemat As TextBox) dr.Close() cmd.Connection = con cmd.CommandText = "select name_jens from ajnas where id = " & nkala.Text & "" dr = cmd.ExecuteReader() While dr.Read nkala.Text = dr.Item(0) End While 'por kardane combobox e gheymat dr.Close() cmd.CommandText = "select gheymate_forush from ajnas where id = " & ckala.Text & "" dr = cmd.ExecuteReader() While dr.Read If Not IsDBNull(dr.Item(0)) Then ghyemat.Text = dr.Item(0) Else ghyemat.Text = Nothing End If End While End Sub 。我想以Angular的方式使用。

请帮助谢谢

1 个答案:

答案 0 :(得分:3)

您只能访问控制器容器内的范围变量,即您的案例中的div。在该div之外,范围变量未定义。将该标记放在div中,您就可以访问它。

示例:

[Registry]
Root: HKLM; Subkey: SOFTWARE\Electronic Arts; Flags: dontcreatekey uninsdeletekeyifempty
Root: HKLM; Subkey: SOFTWARE\Electronic Arts\Harry Potter and the Deathly Hallows Part 1; Flags: uninsdeletekeyifempty dontcreatekey
Root: HKLM; Subkey: SOFTWARE\Electronic Arts\Harry Potter and the Deathly Hallows Part 1; ValueType: string; ValueName: DisplayName; ValueData: Harry Potter and the Deathly Hallows™ - Part 1; Flags: uninsdeletekey
Root: HKLM; Subkey: SOFTWARE\Electronic Arts\Harry Potter and the Deathly Hallows Part 1; ValueType: string; ValueName: CD Drive; ValueData: E:\; Flags: uninsdeletekey
Root: HKLM; Subkey: SOFTWARE\Electronic Arts\Harry Potter and the Deathly Hallows Part 1; ValueType: string; ValueName: ProductName; ValueData: Harry Potter and the Deathly Hallows™ - Part 1; Flags: uninsdeletekey
Root: HKLM; Subkey: SOFTWARE\Electronic Arts\Harry Potter and the Deathly Hallows Part 1; ValueType: string; ValueName: Folder; ValueData: {group}; Flags: uninsdeletekey
Root: HKLM; Subkey: SOFTWARE\Electronic Arts\Harry Potter and the Deathly Hallows Part 1; ValueType: string; ValueName: Install Dir; ValueData: {app}; Flags: uninsdeletekey
Root: HKLM; Subkey: SOFTWARE\Electronic Arts\Harry Potter and the Deathly Hallows Part 1; ValueType: string; ValueName: Installed From; ValueData: {src}; Flags: uninsdeletekey
Root: HKLM; Subkey: SOFTWARE\Electronic Arts\Harry Potter and the Deathly Hallows Part 1; ValueType: string; ValueName: Locale; ValueData: en_UK; Flags: uninsdeletekey
Root: HKLM; Subkey: SOFTWARE\Electronic Arts\Harry Potter and the Deathly Hallows Part 1; ValueType: string; ValueName: Language; ValueData: English (UK); Flags: uninsdeletekey
Root: HKLM; Subkey: SOFTWARE\Electronic Arts\Harry Potter and the Deathly Hallows Part 1; ValueType: string; ValueName: Product GUID; ValueData: {{C9AAF970-4E7E-4C98-AD67-09C74379D345}; Flags: uninsdeletekey
Root: HKLM; Subkey: SOFTWARE\Electronic Arts\Harry Potter and the Deathly Hallows Part 1; ValueType: string; ValueName: Suppression Exe; Flags: uninsdeletekey
Root: HKLM; Subkey: SOFTWARE\Electronic Arts\Harry Potter and the Deathly Hallows Part 1; ValueType: string; ValueName: Registration; ValueData: Software\Electronic Arts\Electronic Arts\Harry Potter and the Deathly Hallows Part 1\ergc; Flags: uninsdeletekey
Root: HKLM; Subkey: SOFTWARE\Electronic Arts\Harry Potter and the Deathly Hallows Part 1\1.0; ValueType: string; ValueName: DisplayName; ValueData: Harry Potter and the Deathly Hallows Part 1; Flags: uninsdeletekey
Root: HKLM; Subkey: SOFTWARE\Electronic Arts\Harry Potter and the Deathly Hallows Part 1\1.0; ValueType: dword; ValueName: Language; ValueData: $00000013; Flags: uninsdeletekey
Root: HKLM; Subkey: SOFTWARE\Electronic Arts\Harry Potter and the Deathly Hallows Part 1\1.0; ValueType: string; ValueName: LanguageName; ValueData: English (UK); Flags: uninsdeletekey
Root: HKLM; Subkey: SOFTWARE\Electronic Arts\Harry Potter and the Deathly Hallows Part 1\1.0; ValueType: string; ValueName: ProgramGroup; ValueData: {group}; Flags: uninsdeletekey