如何检查angularJS中是否选择了某个项目?

时间:2015-12-27 08:56:04

标签: angularjs angularjs-templates

我尝试在用户选择项目名称后弹出警告消息:' MakeGray'从下拉菜单中。但是,我甚至无法运行该功能。我在函数内部放置了一条警告消息,以便在访问它的瞬间运行,但是不会弹出这样的消息。

JS

app.controller('MainController',['$scope', '$http', 'API', function($scope, $http, API){
    $scope.imageUrl = "";

    $scope.template = "";
    $scope.templates =[ 

    {name: 'select an option...'},
    {name: 'MakeGray', url:'opencv/MakeGray/MakeGray.html'},
    {name: 'Canny'},

    ];    

    $scope.template = $scope.templates[0];

    $scope.Opencv = function(template){
    alert("HI");
        if (template.name === 'MakeGray'){
            alert("MAKEGRAY");
        }
    }
}]);

HTML

<html ng-app="app">
  <head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="css/bootstrap.min.css" />
    <link rel="stylesheet" href="css/opencv_css.css" />
  </head>

  <nav class="navbar navbar-default">
    <div class="container">                 <!-- top intro part -->
      <div class="navbar-header">
    <a class="navbar-brand" href="#/">  OPENCV 3.0.0</a>
      </div>

      <ul class="nav navbar-nav navbar-right">
    <li><a href="#/"><i class="fa fa-home"></i> Home</a></li>
    <li><a href="#/about"><i class="fa fa-shield"></i> About</a></li>
    <li><a href="#/contact"><i class="fa fa-comment"></i> Contact</a></li>
      </ul>
    </div>
  </nav>

  <body ng-controller="MainController">

    <div class="row"> <!-- Dropdown menu  -->

      <div class="col-md-20">
    <div id="main">
      <form class="form-horizontal" role="form">
        <label class="control-label col-md-2">Filter List:</label>
        <div class="col-md-5">

          <select id="form_filter" class="form-control"
              ng-model="template"
              ng-options="t.name for t in templates" <!-- why did t.name as t for t in templates fail? -->
              ng-change="Opencv(template)">
          </select>
        </div>
      </form>
    </div>
      </div>

      <input type="file" id="form_img" name="fileToUpload">
      <button ng-click="add()">Upload</button>   

    </div>    

    <!--

    Display Images here
    moves images about 10 pixels below the upload button 

      -->

    <br><br> 

    <div class="row">

      <div class="col-md-push-2 col-sm-push-1 col-lg-push-1 col-md-8-col-sm-8 col-lg-6 text-warning">
    <div id="imagesData">     
      <img id="img1" src="" alt="" class="data-image" />

    </div>
      </div>

      <div id="imagesData">   
    <img id="img2" src="" alt="" class="data-image" />
      </div>     

    </div>

    <!-- 
     Display html of selected opencv filter 
      -->

    <br>

    <div ng-include="template.url"></div>

    <script src="js/jQuery.js"></script>
    <script src="js/angular.js"></script>
    <script src="js/ui-bootstrap-tpls-0.14.3.min.js"></script>    
    <script src="js/app.js"></script>

  </body>
</html>

3 个答案:

答案 0 :(得分:2)

问题是由HTML中的评论引起的:

<!-- why did t.name as t for t in templates fail? -->

删除它,一切都会按预期工作。

关于此评论中的问题,答案是:因为as之后的内容是选择框的标签,之前的是模型。所以你真的想要

t as t.name for t in templates

Demo plunkr

答案 1 :(得分:0)

尝试这样(ng-change =&#34; Opencv()&#34; if if($ scope.template ===&#39; MakeGray&#39;)) -

#include <stdio.h>
#include <string.h>    //strlen
#include <stdlib.h>    //strlen
#include <stdbool.h>
#include <time.h>
#include <sys/socket.h>
#include <arpa/inet.h> //inet_addr
#include <unistd.h>    //write
#include <pthread.h> //for threading , link with lpthread
#define N 2 // player numbers
#define CardUpperLimit 10;

// Game setting 
int n = 1000; 
int flag_phase1 = 1;
pthread_t sniffer_thread[N];
char message[256] , client_message[2000];
int PHASE=0; 
int player_index=0;

void *test(void *);
void error(char *msg)
{
    perror(msg);
    exit(1);
}

int main(int argc , char *argv[])
{
    // Socket setting
    int i,j;
    int socket_desc , client_sock , c , *new_sock;
    struct sockaddr_in server , client;
    int whoesTurn;

    //Create socket
    socket_desc = socket(AF_INET , SOCK_STREAM , 0);
    if (socket_desc == -1)
    {
        printf("Could not create socket");
    }
    puts("Socket created");

    //Prepare the sockaddr_in structure
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = INADDR_ANY;
    server.sin_port = htons( 8888 );

    //Bind
    if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
    {
        perror("bind failed. Error");
        return 1;
    }
    puts("Bind done");

    //Listen
    listen(socket_desc , 3);
    c = sizeof(struct sockaddr_in);
    //Accept and incoming connection
    puts("Waiting for incoming connections...");
    c = sizeof(struct sockaddr_in);

    /* Prepare section.. */
    while( (client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c)) )
    {
        puts("Connection accepted");
        new_sock = malloc(1);
        *new_sock = client_sock;

        if( pthread_create( &sniffer_thread[player_index] , NULL ,  test , (void*) new_sock) < 0)
        {
            perror("could not create thread");
            return 1;
        }

        player_index+=1;

        //  break if all players entry..
        if ( player_index== N ) break;
    }
        printf("All players are in the game!\n");

        /* Phase 1 ... */
        PHASE=1;
        for ( whoesTurn = 0; whoesTurn < N; whoesTurn++)
        {
            /* Communication! */
            bzero(client_message, 256);
            n=whoesTurn+1;
            while(flag_phase1); // busy waiting until this thread function finish..
            flag_phase1=1;
            printf("Finish Communication!!\n");

        }
        n=0;  // reset checking index
        printf("First Phase done..\n");

        /* Phase 2 .. */
        PHASE+=1;
        for (whoesTurn=0;whoesTurn < N; whoesTurn++)
        {
            /* Communication Part! */
            bzero(client_message, 256);
            n=whoesTurn+1;
            while(flag_phase1) {usleep(100);}  // busy waiting until this thread function finish..
            flag_phase1=1;
            printf("Finish Communication!!\n");

        }
        n=0;

        PHASE=0;
        while(1) 
            sleep(1);

    if (client_sock < 0)
    {
        perror("accept failed");
        return 1;
    }

    return 0;
}

void * test(void *socket_desc)
{
    int i, j;
    int sock = *(int*)socket_desc;
    int read_size;
    int player_index_handler = player_index;

    while(1)
    {
        /* Communication */
        if(n==player_index_handler)
        {
            printf("I am player:%d\n", player_index_handler); 
            if (PHASE==1)
                *message = '1';
            else if (PHASE==2)
                *message = '2';
            write(sock, message, strlen(message));
            printf("[%d],Phase msg sent:%d\n", player_index_handler,atoi(message));

            read_size = recv(sock , client_message , 2000 , 0);
            printf("the client_message is:%s, from player %d\n", client_message, player_index_handler);
            bzero(message,256);

            // Finish works..
            flag_phase1=0;
        }

    }
} 

答案 2 :(得分:0)

这是工作Plunker

我刚刚更改了下面的下拉列表

<select ng-model="template"  ng-change="Opencv(template)" ng-options="c as (c.url + ' - ' + c.name) for c in templates">
</select>